strcpy differences under HP-UX and Linux
Hi All,
Following code works perfect under HP-UX. #include <string.h> int main() { char a[10]; char *b = NULL; strcpy(a, b); } But same code crash under linux (Red Hat). It’s crashing with obvious reasons. Best solution will be put the check before each strcpy and throw the error in case of 2nd arg pointing to NULL. Currently we are doing the HP-UX to Linux migration, and it will be very hard to do these changes in all the places. Any suggestions? Regards, Amar |
Quote:
Quote:
Regards Rupert |
Quote:
When your code will gift you the "segmentation fault" ,the debugger will lead your way and point you to the buggy code. That way you'll spend less time hunting for bugs in the large code. and use code tags for posting code in the forum: http://www.linuxquestions.org/questi...do=bbcode#code |
Yes Code is broken agreed. But it's a fact it's working fine under HP-UX. May be because of smart compiler of HP-UX.
I agree, code is not written good. And this problem will not be only with strcpy, it’s going to be with all string operation. HP-UX given the flexibility to do this mistake, and guys are doing it since last 10-15 years without any problem. Now we are migrating to the Linux (just because of cost cutting). Fix for this problem is very easy, but it’s 20-30 thousand files, I don’t remember how many string operations are there. Fix one by one all the string operation will take years, so looking for some other smart solution like re-writing the string function again to handle NULL pointer. eg. strcpy(char *a, char *b) { if(a == NULL) throw error; if(b == NULL) throw error; original.strcpy(a,b); } Just looking for some good suggestion. |
Addition to above comment, strcpy(a, b) is working fine in case of b is NULL, but crashing (I hope should crash everywhere) where a is NULL.
|
Quote:
|
Yes, you are right. But it's actually not ignoring.
It's just putting a[0] = '\0'; which is exactly our implemetation is looking for. I think because of this no one notice this problem even exist. Now only because of Linux we started to get this problem. Code is nearly about 10-15 year old. |
Hi @amarg, I'm here in 2023 with same issue. Porting legacy C code from HP-UX to RHEL and foudn that, HP-UX complier can derefrence NULL pointer inside string operation methods like strcpy, strlen. However, RHEL GCC complier SegFaluts.
What solution applied you back then to resolve this issue with derefrencing NULL pointer for RHEL GCC complier. Any wrapper method for legacy strcpy()? Thanks for your suggestions. |
Aix is another example for a platform, where address zero is readable (the kernel starts at address zero).
There is no universal solution, the bugs have to be found one by one. The good news is that you can use valgrind on Linux: it will find many other problems as well (e.g. uninitalized variables). PS: if your old platform is big endian, then expect some problems on little endian platform x86 (or amd64). PPS: Current gcc/clang compilers give you many useful warnings, I suggest you fix them all. |
Quote:
That code is just broken and useless (not strcpy, but where is it called from). So you need to inspect that one by one (line by line) and find out what was the original intention. There is only one general way to avoid segfault: define (overwrite) strcpy to check first a and b and do nothing if any of them is NULL. But I don't think your code will work that way. |
I think I know what the code does. Replace it with:
Code:
#include <string.h> Code:
int main() { Quote:
Code:
❯ cat init.c |
Quote:
Maybe use sed to do something like this: Code:
sed 's/strcpy(a, b);/strcpy(a, (b == NULL ? "" : b));/' Code:
char *hpstrcpy(char *a, char *b) |
Quote:
Quote:
|
All times are GMT -5. The time now is 09:17 PM. |