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.
On 2025-04-02, Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
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.
Let us start with ISO C 90.
7.1.7 Use of library functions
Each of the following statements applies unless explicitly stated
otherwise in the detailed descriptions that follow. If an argument to
a function has an invalid value (such as a value outside the domain of
the function. or a pointer outside the address space of the program.
or a null pointer), the behavior is undefined.
^^^^^^^^^^^^^^^^^
It gets more verbose and indented, but the null treatment is
consistently there.
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?
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:[snip]
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.
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.
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?
It gets more verbose and indented, but the null treatment is
consistently there.
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.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (0 / 16) |
Uptime: | 169:00:39 |
Calls: | 10,385 |
Calls today: | 2 |
Files: | 14,057 |
Messages: | 6,416,551 |