Skip to contentSkip to conversation

New: Example in getpwnam_r man page compares size_t variable to -1

Bug 215097 [email protected] 1 participant 4 messages Nov 22, 2021 – Nov 23, 2021 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

https://bugzilla.kernel.org/show_bug.cgi?id=215097

Bug ID: 215097
Summary: Example in getpwnam_r man page compares size_t
variable to -1
Product: Documentation
Version: unspecified
Hardware: All
OS: Linux
Status: NEW
Severity: low
Priority: P1
Component: man-pages
Assignee: [email protected]
Reporter: [email protected]
Regression: No

The getpwnam_r man page
(https://man7.org/linux/man-pages/man3/getpwnam_r.3.html) has this in the example code:

           size_t bufsize;
           ...

           bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
           if (bufsize == -1)          /* Value was indeterminate */
               bufsize = 16384;        /* Should be more than enough */

bufsize is a variable with unsigned type, so this results in:

mantest.c: In function 'main':
mantest.c:14:20: warning: comparison of integer expressions of different
signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
14 | if(bufsize == -1)

Signature · 4 lines
You may reply to this email to add a comment.

You are receiving this mail because:
You are watching the assignee of the bug.
#1 lore raw

[Bug 215097] Example in getpwnam_r man page compares size_t variable to -1

https://bugzilla.kernel.org/show_bug.cgi?id=215097

Alejandro Colomar (man-pages) ([email protected]) changed:

What |Removed |Added
----------------------------------------------------------------------------
CC| |[email protected]

--- Comment #1 from Alejandro Colomar (man-pages) ([email protected]) --- Yes, long (sysconf(3) uses that) or ssize_t (to indicate a size) seem better types. Which one do you prefer?

Also I noticed that sysconf(3) doesn't document _SC_GETPW_R_SIZE_MAX.

Signature · 4 lines
You may reply to this email to add a comment.

You are receiving this mail because:
You are watching the assignee of the bug.
#2 lore raw

[Bug 215097] Example in getpwnam_r man page compares size_t variable to -1

In reply to [email protected]

https://bugzilla.kernel.org/show_bug.cgi?id=215097

--- Comment #2 from Fabian ([email protected]) --- (In reply to Alejandro Colomar (man-pages) from comment #1)

Quoted 2 lines

Yes, long (sysconf(3) uses that) or ssize_t (to indicate a size) seem better types. Which one do you prefer?

IMO using "long" makes the most sense. It's already used as the return value of sysconf, while ssize_t has no direct relevance to the code.

Quoted 1 line

Also I noticed that sysconf(3) doesn't document _SC_GETPW_R_SIZE_MAX.

Oh, indeed!

Signature · 4 lines
You may reply to this email to add a comment.

You are receiving this mail because:
You are watching the assignee of the bug.
#3 lore raw

[Bug 215097] Example in getpwnam_r man page compares size_t variable to -1

In reply to [email protected]

https://bugzilla.kernel.org/show_bug.cgi?id=215097

--- Comment #3 from Alejandro Colomar (man-pages) ([email protected]) --- Makes sense. Fixed.
I'll keep the bug open until I fix sysconf(3) too.

Thanks!
Alex

getpwnam.3: EXAMPLES: Fix signedness

sysconf(3) returns a long. Since it can return -1 (and we're making use of that value), we can't use size_t for bufsize. Use long.

man3/getpwnam.3 +1 -1
index 71457a916..8ca13f1a2 100644
@@ -304,7 +304,7 @@ main(int argc, char *argv[])
     struct passwd pwd;
     struct passwd *result;
     char *buf;
-    size_t bufsize;
+    long bufsize;
     int s;
 
     if (argc != 2) {
Signature · 4 lines
You may reply to this email to add a comment.

You are receiving this mail because:
You are watching the assignee of the bug.