Deutsch   English   Français   Italiano  
<865xjmhyih.fsf@linuxsc.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups: comp.lang.c
Subject: Re: A question regarding C string functions
Date: Wed, 02 Apr 2025 17:36:22 -0700
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <865xjmhyih.fsf@linuxsc.com>
References: <vsjujs$21log$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Thu, 03 Apr 2025 02:36:24 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5fae27e4725ec0f03e8fd915a6650b3a";
	logging-data="3219972"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19G12/UJo9zBtj8RPw+PKRAQeAvPw+x9NY="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:k7TCU15aruw+MWQZ9f8f880P8Dg=
	sha1:QJSPA7jckZo+cIzkKw6xI/W8P/0=
Bytes: 2540

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.