New: strchr(3) and memchr(3) should explain behaviour when character 'c' is '\0'.
https://bugzilla.kernel.org/show_bug.cgi?id=42042
Summary: strchr(3) and memchr(3) should explain behaviour when
character 'c' is '\0'.
Product: Documentation
Version: unspecified
Platform: All
OS/Version: Linux
Tree: Mainline
Status: NEW
Severity: normal
Priority: P1
Component: man-pages
AssignedTo: documentation_man-pages-ztI5WcYan/[email protected]
ReportedBy: [email protected]
Regression: No
PROBLEM
-------
strchr(3) and memchr(3) do not explain the behaviour if the character to search for is specified as a null character ('\0'). According to my copy of Harbison and Steele, since the terminator is considered part of the string, a call such as:
strchr("hello", '\0')
... will return the address of the terminating null in the specified string.
RATIONALE
---------
strchr(3) and memchr(3) are inconsistent with index(3) which states:
"The terminating NULL character is considered to be a part of the strings."
Adding such a note to strchr(3) and memchr(3) is also important since it is not unreasonable to assume that strchr() will return NULL in this scenario. This leads to code like the following which is guaranteed to fail should get_a_char() return '\0':
char string[] = "hello, world"; int c = get_a_char();
if (! strchr(string, c))
fprintf(stderr, "failed to find character in string\n");
SUGGESTED UPDATE
----------------
I'd suggest adding something like the following to strchr(3) and memchr(3):
If 'c' is '\0' (the terminating null character), strchr() will return the address of the terminating null character in 's'.
Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are watching the assignee of the bug. -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
[Bug 42042] strchr(3) and memchr(3) should explain behaviour when character 'c' is '\0'.
In reply to a message not in the archivehttps://bugzilla.kernel.org/show_bug.cgi?id=42042
Andrew Morton <akpm-de/[email protected]> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |akpm-de/[email protected]
--- Comment #1 from Andrew Morton <akpm-de/[email protected]> 2011-08-30 19:36:38 --- Send us a patch ;)
Documentation/SubmittingPatches has some guidelines.
Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are watching the assignee of the bug. -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
[Bug 42042] strchr(3) and memchr(3) should explain behaviour when character 'c' is '\0'.
In reply to a message not in the archivehttps://bugzilla.kernel.org/show_bug.cgi?id=42042
--- Comment #2 from James Hunt <[email protected]> 2011-08-31 09:04:06 --- Created an attachment (id=70952)
--> (https://bugzilla.kernel.org/attachment.cgi?id=70952)
update to strchr.3 and memchr.3
Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are watching the assignee of the bug. -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
[Bug 42042] strchr(3) and memchr(3) should explain behaviour when character 'c' is '\0'.
In reply to a message not in the archivehttps://bugzilla.kernel.org/show_bug.cgi?id=42042
--- Comment #3 from James Hunt <[email protected]> 2011-08-31 09:05:48 --- Created an attachment (id=70962)
--> (https://bugzilla.kernel.org/attachment.cgi?id=70962)
test program showing behaviour of strchr, strrchr, memchr, strchrnul, and
strstr.
Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are watching the assignee of the bug. -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
[Bug 42042] strchr(3) and memchr(3) should explain behaviour when character 'c' is '\0'.
In reply to a message not in the archivehttps://bugzilla.kernel.org/show_bug.cgi?id=42042
--- Comment #4 from James Hunt <[email protected]> 2011-08-31 09:15:31 --- Test program run on:
- Ubuntu Natty (11.04) system with libc6 version 2.13-0ubuntu13 (egcs).
- Fedora 15 system with glibc version 2.13.90-9.
Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are watching the assignee of the bug. -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
[Bug 42042] strchr(3) and memchr(3) should explain behaviour when character 'c' is '\0'.
In reply to a message not in the archivehttps://bugzilla.kernel.org/show_bug.cgi?id=42042
--- Comment #5 from James Hunt <[email protected]> 2011-09-01 08:04:46 --- The BSD folk already have this behaviour documented in their man pages:
Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are watching the assignee of the bug. -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
[Bug 42042] strchr(3) and memchr(3) should explain behaviour when character 'c' is '\0'.
In reply to a message not in the archivehttps://bugzilla.kernel.org/show_bug.cgi?id=42042
Michael Kerrisk <[email protected]> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |[email protected]
--- Comment #6 from Michael Kerrisk <[email protected]> 2012-04-23 20:32:31 --- James,
Thanks for the detailed report, and sorry I've been slow to respond.
For strchr(3), I applied a variation of what you proposed:
man3/strchr.3 +6 -1
@@ -28,7 +28,7 @@ .\" 2006-05-19, Justin Pryzby <pryzbyj-4haIdLZGnXT/[email protected]> .\" Document strchrnul(3). .\" -.TH STRCHR 3 2010-09-20 "GNU" "Linux Programmer's Manual" +.TH STRCHR 3 2012-04-24 "GNU" "Linux Programmer's Manual" .SH NAME strchr, strrchr, strchrnul \- locate character in string .SH SYNOPSIS
@@ -72,6 +72,11 @@ and .BR strrchr () functions return a pointer to the matched character or NULL if the character is not found. +The terminating null byte is considered part of the string, +so that if +.I c +is specified as \(aq\\0\(aq, +these functions return a pointer to the terminator. The .BR strchrnul ()
Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are watching the assignee of the bug. -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
[Bug 42042] strchr(3) and memchr(3) should explain behaviour when character 'c' is '\0'.
In reply to a message not in the archivehttps://bugzilla.kernel.org/show_bug.cgi?id=42042
--- Comment #7 from Michael Kerrisk <[email protected]> 2012-04-23 20:36:56 --- James,
Regarding memchr(), I'm not sure a change is warranted. memchr() is defined in terms of 'bytes' and 'memory areas', rather than strings, so I'd have said it would take an obtuse reading to consider that '\0' is interpreted specially (whereas, as you say, one could be left doubtful about what strchr() does when c is '\0').
I'd guess that this is also why POSIX and the FreeBSD man page make no statement on this pont.
So, I'm inclined not to make a change.
Thanks,
Michael
Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are watching the assignee of the bug. -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
[Bug 42042] strchr(3) and memchr(3) should explain behaviour when character 'c' is '\0'.
In reply to a message not in the archivehttps://bugzilla.kernel.org/show_bug.cgi?id=42042
--- Comment #8 from Michael Kerrisk <[email protected]> 2012-04-23 20:46:33 --- One further note about memchr(3) though. It does talk about the argument c as a
*character*, and rawmemchr() also talks about strings, which clouds the issue
somewhat. I'll do some rewrites there to refer rather to "bytes" and "memory
areas".
Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are watching the assignee of the bug. -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
[Bug 42042] strchr(3) and memchr(3) should explain behaviour when character 'c' is '\0'.
In reply to a message not in the archivehttps://bugzilla.kernel.org/show_bug.cgi?id=42042
--- Comment #9 from Michael Kerrisk <[email protected]> 2012-04-23 21:19:03 --- James, all changes are pushed to kernel.org git now.
I'll close this now. Please reopen if you thing there's more to be said about memchr().
Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are watching the assignee of the bug. -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
[Bug 42042] strchr(3) and memchr(3) should explain behaviour when character 'c' is '\0'.
In reply to a message not in the archivehttps://bugzilla.kernel.org/show_bug.cgi?id=42042
Michael Kerrisk <[email protected]> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |CODE_FIXSignature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are watching the assignee of the bug. -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html