🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.os.linux.help
4 messages
4 total messages Started by Christopher W. Curtis Thu, 14 Mar 1996 04:09
strncpy bug?
#3
Author: Christopher W. Curtis
Date: Thu, 14 Mar 1996 04:09
17 lines
969 bytes
Hi.

I was wondering if this is a Linux thing (kernel 1.2.1) or a
more general 'problem'.  If you call strncpy as such:
	strncpy( dest, NULL, x );
it will seg fault.  Is this common among all platforms, or
just with Linux?

The man page says nothing about it, but I would have presumed
that if str[n]cpy were passed NULL as a source, it would
merely catenate the destination.  Did I presume wrong?

Thanks,
--
Christopher Curtis, Sun SysAdmin - http://www.ee.fit.edu/users/ccurtis
Florida Institute of Technology  - telnet bofh.engr.wisc.edu 666
Melbourne, Florida  USA          - Member, Team OS/2
Re: strncpy bug?
#6
Author: Falstaff
Date: Thu, 14 Mar 1996 09:46
26 lines
1447 bytes
ccurtis@ee.fit.edu (Christopher W. Curtis) writes:

>Hi.

>I was wondering if this is a Linux thing (kernel 1.2.1) or a
>more general 'problem'.  If you call strncpy as such:
>	strncpy( dest, NULL, x );
>it will seg fault.  Is this common among all platforms, or
>just with Linux?

>The man page says nothing about it, but I would have presumed
>that if str[n]cpy were passed NULL as a source, it would
>merely catenate the destination.  Did I presume wrong?

If the strncpy() manual does not report any special cases, you should
assume that there are none.
For example, free() supports NULL as a special no-op case, but
strncpy() just tries to copy a string from address 0.
As this is generally not correct, Linux will trap accesses to address
0, so you can immediately see your program has an error.

Frank
--
The famous GIICM now on line:  http://www.xs4all.nl/~falstaff/GIICM.html
------------------------------------------------------------------------
Frank A. Vorstenbosch        +31-(70)-355 5241        falstaff@xs4all.nl
Re: strncpy bug?
#4
Author: Jochen Schoof
Date: Thu, 14 Mar 1996 11:43
38 lines
2279 bytes
Christopher W. Curtis (ccurtis@ee.fit.edu) wrote:
: I was wondering if this is a Linux thing (kernel 1.2.1) or a
: more general 'problem'.  If you call strncpy as such:
: 	strncpy( dest, NULL, x );
: it will seg fault.  Is this common among all platforms, or
: just with Linux?
:
: The man page says nothing about it, but I would have presumed
: that if str[n]cpy were passed NULL as a source, it would
: merely catenate the destination.  Did I presume wrong?

My man page says:

       The  strcpy() function copies the string pointed to be src
       (including the terminating `\0' character)  to  the  array
       pointed  to by dest.  The strings may not overlap, and the
       destination string dest must be large  enough  to  receive
       the copy.

       The  strncpy()  function  is similar, except that only the
       first n bytes of src are copied.

When passing NULL as the src pointer, it definetly does not reference
a string. Therefore NULL is not a legal parameter to strcpy() and
strncpy(). Your expectation of what should happen is true for the
empty string (i.e. ""), but this is completely different from NULL.

- Jochen

--
--------------------------------------------------------------------------
 Jochen Schoof                  mailto:schoof@informatik.uni-wuerzburg.de
 Lehrstuhl fuer Informatik II +-------------------------------------------
 Universitaet Wuerzburg       | You are just reading a .sig-light:
 D-97074 Wuerzburg (Germany)  | It is free of fat, sugar and cholesterol!
------------------------------+-------------------------------------------
 WWW-Homepage:        http://www.informatik.uni-wuerzburg.de/staff/joscho
--------------------------------------------------------------------------
Re: strncpy bug?
#5
Author: Tanmoy Bhattacharya
Date: Thu, 14 Mar 1996 15:25
32 lines
1912 bytes
In article <ccurtis.826776589@ee.fit.edu>
ccurtis@ee.fit.edu (Christopher W. Curtis) writes:

<snip>
CWC: I was wondering if this is a Linux thing (kernel 1.2.1) or a
CWC: more general 'problem'.  If you call strncpy as such:
CWC: 	strncpy( dest, NULL, x );
CWC: it will seg fault.  Is this common among all platforms, or
CWC: just with Linux?
CWC: 
CWC: The man page says nothing about it, but I would have presumed
CWC: that if str[n]cpy were passed NULL as a source, it would
CWC: merely catenate the destination.  Did I presume wrong?

Yes. The standard library functions should usually always be passed
pointers which actually point to objects of appropriate types. A null
pointer points nowhere: and is therefore invalid in most of these
contexts. In this context, you may pass the constant string ""
instead. 

Some library functions specifically allow you to pass in null
pointers: that is obviously different.

Cheers
Tanmoy
--
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads