C strchr在HPUX上使用NULL值,但在RHEL上使用segfaults [英] C strchr works with NULL value on HPUX but segfaults on RHEL

查看:143
本文介绍了C strchr在HPUX上使用NULL值,但在RHEL上使用segfaults的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些代码从HPUX 11.11移到RHEL 7.5,它包括一个使用strchr的函数.在HPUX上运行良好,在RHEL上存在分段错误.我隔离了代码,并创建了以下简单测试以及后续结果.找不到字符时,HPUX strchr似乎返回空字符串而不是NULL.这不是手册页所说的.我发现它可能不是strchr函数,而是HPUX处理NULL值的方式的不同,或从cc到gcc的编译器的不同.真的有人知道这里发生了什么吗?为什么在HPUX上代码无法分段?

I'm moving some code from HPUX 11.11 to RHEL 7.5 and it includes a function that uses strchr. On HPUX it runs fine, and on RHEL there is a segmentation fault. I isolated the code and created the following simple test, with the subsequent results. It looks like HPUX strchr is returning an empty string rather than NULL when the character is not found. This is not what the man page says. I have found it might not be the strchr function but the difference in how HPUX handles NULL values, or a difference in compiler from cc to gcc. Does anyone actually know what's happening here? Why is the code not segfaulting on HPUX?

C代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
    int i = 0;
    char* phrase = "Here is my phrase.";
    while (i < 5){
        phrase = strchr(phrase, ' ');
        ++phrase;

        ++i;
    }
    return 0;
}

HPUX makefile:

HPUX makefile:

BIN=/path/to/bin/
CFLAGS=-c

#----------Targets----------#
default: $(BIN)strchrtest

#----------Objects----------#
OBJS = strchrtest.o

#----------------BUILD----------------#
$(BIN)strchrtest: $(OBJS) 
    cc -o $@ $(OBJS) 

strchrtest.o: strchrtest.c
    cc $(CFLAGS) strchrtest.c

RHEL生成文件:

CC=gcc
BIN=/path/to/bin/
CFLAGS=-c -g -Wall

#----------Targets----------#
default: $(BIN)strchrtest

#----------Objects----------#
OBJS = strchrtest.o

#----------------BUILD----------------#
$(BIN)strchrtest: $(OBJS) 
    $(CC) -o $@ $(OBJS) 

strchrtest.o: strchrtest.c
    $(CC) $(CFLAGS) strchrtest.c

HPUX只是一个成功的结果.没有分割错误.

HPUX is just a successful result. No segmentation fault.

RHEL结果:

(gdb) run
Starting program: ../strchrtest

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b4c5f4 in __strchr_sse42 () from /lib64/libc.so.6

复制下面的评论:重点是,当要求strchr函数本身在空指针中找到一个字符(已递增)时,它会导致段错误,但是在HPUX上却没有.因此,它要么返回一个空字符串,然后在下一个循环中将其传递给strchr,要么strchr通过不进行段错误处理以不同方式处理null指针参数.否则我会误会正在发生的事情.

Copied form my comment below: The point is that the strchr function itself is causing the segfault when asked to find a character in the null pointer (that has been incremented), but on HPUX it doesn't. So it's either returning an empty string that is then passed strchr on the next loop, or strchr is handling the null pointer parameter differently by not segfaulting. Or I misunderstand what's happening.

推荐答案

像这样使两个文件r.c,main.c r.c:

Make two files, r.c, main.c as such r.c:

int *r = 0;

main.c:

extern int *r;
int main(void) {
     return *r;
}

然后cc main.c r.c; ./a.out 如果不是sigsegv,则您的运行时正在为您映射一个null页. 您可以自己完成-main.c:

then cc main.c r.c; ./a.out if it doesn't sigsegv, your runtime is mapping a page of nulls for you. you could do it yourself - main.c:

#include <sys/mman.h>
int main(void) {
    p = mmap(0, 1, PROT_READ, MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1, 0);
    if (p == MAP_FAILED) {
        perror("mmap");
        exit(1);
    }
     ....
}

但是至少在ubuntu上,您需要是root:(

but at least on ubuntu, you need to be root :(

这篇关于C strchr在HPUX上使用NULL值,但在RHEL上使用segfaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆