Re: Fwd: getline() manpage typo

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello Stefan,

On 1/19/22 19:53, Stefan Rohrbacher wrote:
> To whom it may concern
> 
> Dear Sir or Madam,
> 
> I am writing to you because I believe I have found a typo in the
> manpage for the getline() function provided by stdio.h.
> I am using version 5.12 on Fedora 35 but it is also present in the
> current version 5.13.
> The typo is  located in the "EXAMPLES" section at the bottom, there
> the variable "nread" in line 10 is of type "ssize_t" and I believe it
> is supposed to be of type "size_t":
> 
>        [...]
>        int
>        main(int argc, char *argv[])
>        {
>            FILE *stream;
>            char *line = NULL;
>            size_t len = 0;
>            ssize_t nread; <-- supposed typo here!
> 
>        [...]

I'm not sure why you believe this.  The prototype for getline(3) is:

       ssize_t getline(char **restrict lineptr, size_t *restrict n,
                       FILE *restrict stream);

which uses 'ssize_t'.  Or checking the glibc sources:

alx@ady2:~/src/gnu/glibc$ grep_glibc_prototype getline
libio/stdio.h:621:
extern __ssize_t getline (char **__restrict __lineptr,
                          size_t *__restrict __n,
                          FILE *__restrict __stream) __wur;

So, if we assign the result of getline(3) to a variable, it should be of
type 'ssize_t'.  After that, we use the variable as input to fwrite(3),
which uses 'size_t', but using that type would be incorrect, since we
would be transforming the error code into a huge positive value, which
would be wrong.  And since non-error 'ssize_t' values are always valid
'size_t' values, there's no problem in passing a 'ssize_t' to a 'size_t'
(after doing the proper error checking).

In general, avoid using unsigned types for things that are not bitfields
(or need modular arithmetic).  Using mistakenly 'ssize_t' instead of
'size_t' is usually not dangerous.  Doing the reverse, using mistakenly
'size_t' instead of 'ssize_t' is _very_ dangerous.  This also applies to
other types with signed and unsigned counterparts.

Anyway, thanks for reporting potential bugs!

Cheers,

Alex

-- 
Alejandro Colomar
Linux man-pages maintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/



[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux