Sujet : Re: A question regarding C string functions
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.cDate : 03. Apr 2025, 01:36:22
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <865xjmhyih.fsf@linuxsc.com>
References : 1
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Lew Pitcher <
lew.pitcher@digitalfreehold.ca> writes:
I cannot find any definitive statement in my copies of the various
C language standards that addresses the behaviour of the C string
functions when given a NULL pointer.
>
Specifically, what does the C standard dictate about the behaviour of
strrchr(NULL,'/')
but the question could apply to any of the string functions
(strlen(NULL), etc.)
>
My gut impression is that
strrchr(NULL,'/'), etc
invoke undefined behaviour, and should be avoided.
>
Can anyone comment?
There's a general principle when reading the C standard, which
is, when looking at some leaf section, go up the tree and read
the parent section, and also read the grandparent section, and so
on.
In N1256, strrchr() is defined in 7.21.5.5, and strlen() is
defined in 7.21.6.3. Both 7.21.5 and 7.21.6 are empty (except
for subsections), as is 7.21 itself, but directly underneath
section 7.21 is section 7.21.1 "String function conventions", two
paragraphs long, and this section does mention null arguments.
As it turns out neither of these paragraphs addresses your question
specifically, but the second paragraph does mention 7.1.4, where an
answer may be found. So if the general principle mentioned above
had been followed, there is a reasonable chance you would have found
an answer without having to ask.
Incidentally, I learned this principle myself from another helpful
poster (I don't remember who) in comp.lang.c, and am happy to pass
along what has been for me helpful advice.