🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #311

Re: Interpretation question: Pointers to explicitly zero-length strings

#311
From: david.f.prosser
Date: Mon, 10 Jul 1989 14:01
29 lines
1905 bytes
In article <65745@yale-celray.yale.UUCP> leichter@CS.YALE.EDU (Jerry Leichter) writes:
>A number of the ANSI C library functions accept a string argument, together
>with a maximum length.  Obvious examples include strncpy and printf (%s can
>accept a maximum length).  If the maximum length given is zero, must the
>pointer passed be valid?  In particular, may it be 0, as in:
>
>	strncpy(s,NULL,0);
>
>This may sound like a silly question if you look only at an isolated example
>with constants in it, but it arises naturally when dealing with strings
>represented using a (length,pointer) structure.  When the length is 0, it
>makes little difference where the pointer points, but it is often simplest -
>in initialization, for example - to let it just be 0.
>
>							-- Jerry

The question is certainly not silly.  Unfortunately, the answer is that
there is no guarantee of reasonable behavior in general in these situations.

Only if the pANS has an explicit description that allows for an invalid
pointer value when the length is zero (such as is present for strxfrm)
is passing NULL (or other invalid pointer value) strictly conforming.

The reason is that some functions, when mapped to some architectures' zippy
instructions, may still require valid addresses even if no bytes of any
object need be accessed.  This is particularly the case for functions like
memcpy.

Dave Prosser	...not an official X3J11 answer...
Technical Details
Message-ID: <1052@cbnewsl.ATT.COM>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!att!cbnewsl!dfp
References: <65745@yale-celray.yale.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!att!cbnewsl!dfp
From: dfp@cbnewsl.ATT.COM (david.f.prosser)
Newsgroups: comp.std.c
Subject: Re: Interpretation question:  Pointers to explicitly zero-length strings
Message-ID: <1052@cbnewsl.ATT.COM>
Date: 10 Jul 89 14:01:22 GMT
References: <65745@yale-celray.yale.UUCP>
Reply-To: dfp@cbnewsl.ATT.COM (david.f.prosser)
Organization: AT&T Bell Laboratories
Lines: 29