LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-18-2010, 03:10 AM   #1
amarg
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Rep: Reputation: 0
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
 
Old 09-18-2010, 03:24 AM   #2
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Quote:
Originally Posted by amarg View Post
Following code works perfect under HP-UX.
There is no way a strcpy from a null pointer can work perfect(ly) in C.

Quote:
Originally Posted by amarg View Post
Any suggestions?
  • your code is broken
  • fix it

Regards

Rupert
 
Old 09-18-2010, 03:33 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932
Quote:
Originally Posted by amarg View Post
and it will be very hard to do these changes in all the places.

Any suggestions?
Use the mighty debugger : http://www.gnu.org/software/gdb/documentation/

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
 
Old 09-18-2010, 04:15 AM   #4
amarg
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
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.
 
Old 09-18-2010, 04:16 AM   #5
amarg
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
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.

Last edited by amarg; 09-18-2010 at 04:19 AM.
 
Old 09-18-2010, 04:17 AM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932
Quote:
Originally Posted by amarg View Post
But it's a fact it's working fine under HP-UX. May be because of smart compiler of HP-UX.
A compiler which ignores broken code is DUMB not smart
 
Old 09-18-2010, 04:29 AM   #7
amarg
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
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.
 
Old 05-23-2023, 11:16 AM   #8
mallikarjun_chougule
LQ Newbie
 
Registered: May 2023
Posts: 1

Rep: Reputation: 0
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.
 
Old 05-23-2023, 11:59 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,479
Blog Entries: 1

Rep: Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695
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.

Last edited by NevemTeve; 05-23-2023 at 12:28 PM.
 
1 members found this post helpful.
Old 05-23-2023, 12:51 PM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,224

Rep: Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834
Quote:
Originally Posted by mallikarjun_chougule View Post
Any wrapper method for legacy strcpy()? Thanks for your suggestions.
There is no wrapper. It is just incorrect (strcpy to/from NULL).
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.
 
1 members found this post helpful.
Old 05-23-2023, 05:53 PM   #11
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,954

Rep: Reputation: 5215Reputation: 5215Reputation: 5215Reputation: 5215Reputation: 5215Reputation: 5215Reputation: 5215Reputation: 5215Reputation: 5215Reputation: 5215Reputation: 5215
I think I know what the code does. Replace it with:

Code:
#include <string.h>

int main() {
char a[10];
memset(a, '\0', sizeof(char) * 10);
}
And if it's modern C++, replace it with:

Code:
int main() {
char a[10]{};
}
Of course, this works too:

Quote:
Originally Posted by amarg View Post
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.
If you need the process of finding these errors to go faster, use clang-tidy:

Code:
❯ cat init.c
#include <stdio.h>
#include <string.h>

int main()
{

    char a[10];
    char *b = NULL;

    strcpy(a, b);

    printf("%s\n", a);
}

~/Documents/init via C v13.1.1-gcc 
❯ bear -- gcc init.c

~/Documents/init via C v13.1.1-gcc 
❯ clang-tidy init.c
2 warnings generated.
init.c:10:5: warning: Null pointer passed to 2nd parameter expecting 'nonnull' [clang-analyzer-core.NonNullParamChecker]
    strcpy(a, b);
    ^         ~
init.c:8:5: note: 'b' initialized to a null pointer value
    char *b = NULL;
    ^~~~~~~
init.c:10:5: note: Null pointer passed to 2nd parameter expecting 'nonnull'
    strcpy(a, b);
    ^         ~
init.c:10:5: warning: Call to function 'strcpy' is insecure as it does not provide bounding of the memory buffer. Replace unbounded copy functions with analogous functions that support length arguments such as 'strlcpy'. CWE-119 [clang-analyzer-security.insecureAPI.strcpy]
    strcpy(a, b);
    ^~~~~~
init.c:10:5: note: Call to function 'strcpy' is insecure as it does not provide bounding of the memory buffer. Replace unbounded copy functions with analogous functions that support length arguments such as 'strlcpy'. CWE-119
    strcpy(a, b);
    ^~~~~~
There are no shortcuts beyond this. If fixing the technical debt in this legacy code is going to take years, then, well, that's your estimate.

Last edited by dugan; 05-23-2023 at 06:24 PM.
 
Old 05-25-2023, 07:30 AM   #12
jmccue
Member
 
Registered: Nov 2008
Location: US
Distribution: slackware
Posts: 529
Blog Entries: 1

Rep: Reputation: 313Reputation: 313Reputation: 313Reputation: 313
Quote:
Originally Posted by amarg View Post
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?
I can see that happening if HPUX did something with its version of strcpy().

Maybe use sed to do something like this:

Code:
sed 's/strcpy(a, b);/strcpy(a, (b == NULL ? "" : b));/'
Or better option is create a wrap-around to strcpy() and add that function to all your programs, use sed to change strcpy to hpstrcpy:

Code:
char *hpstrcpy(char *a, char *b)
{
if (b == NULL)
  *a=NULL;
else
  strcpy(a,b);

return(a);
}
good luck

Last edited by jmccue; 05-25-2023 at 08:57 AM. Reason: fixed grammer
 
Old 05-25-2023, 07:43 AM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,707

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Quote:
Originally Posted by jmccue View Post
Maybe use sed to do something like this:

Code:
sed 's/strcpy(a, b);/strcpy(a, (b == NULL ? "" : b));/'
You might consider looking into more specialized tools than sed. For example, I've seen the Linux kernel project reference https://coccinelle.lip6.fr/

Quote:
Coccinelle is a program matching and transformation engine which provides the language SmPL (Semantic Patch Language) for specifying desired matches and transformations in C code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strcpy problem utkarshrawat Programming 11 07-05-2010 11:14 AM
strcpy in java ? Alexander.s Programming 4 08-30-2008 04:03 PM
strcpy causing segfault jpbarto Programming 17 04-07-2004 09:40 PM
strcpy problem rajatgarg Programming 5 11-20-2003 12:46 AM
question with strcpy Jo_Nak Programming 1 07-02-2003 04:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration