Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #391866

Re: A question regarding C string functions

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: A question regarding C string functions
Date 2025-04-02 17:36 -0700
Organization A noiseless patient Spider
Message-ID <865xjmhyih.fsf@linuxsc.com> (permalink)
References <vsjujs$21log$1@dont-email.me>

Show all headers | View raw


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.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

A question regarding C string functions Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2025-04-02 18:11 +0000
  Re: A question regarding C string functions Kaz Kylheku <643-408-1753@kylheku.com> - 2025-04-02 18:33 +0000
    Re: A question regarding C string functions Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2025-04-02 18:36 +0000
    Re: A question regarding C string functions David Brown <david.brown@hesbynett.no> - 2025-04-03 09:29 +0200
  Re: A question regarding C string functions Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-04-02 17:36 -0700
    Re: A question regarding C string functions Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2025-04-03 02:13 +0000
    Re: A question regarding C string functions Ethan Carter <ec1828@somewhere.edu> - 2025-04-03 18:54 -0300
  Re: A question regarding C string functions James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-04-03 00:36 -0400

csiph-web