public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/10780] New: puts(s) where strlen(s) >= INT_MAX returns a negative value other than EOF
@ 2009-10-15  6:48 chris-glibc at chris dot net dot au
  2009-10-29 18:23 ` [Bug libc/10780] " drepper at redhat dot com
  2009-10-29 23:03 ` drepper at redhat dot com
  0 siblings, 2 replies; 3+ messages in thread
From: chris-glibc at chris dot net dot au @ 2009-10-15  6:48 UTC (permalink / raw)
  To: glibc-bugs

Standard C requires the puts() (C99 7.19.7.10) function to return a nonnegative
integer on success and EOF on error. The glibc implementation appears to return
the number of characters written, including the newline. This is valid only
until more than INT_MAX characters are written, because then the value wraps and
becomes negative.

When passed a string of length INT_MAX, then a length of INT_MAX+1 is returned,
which on a two's complement system becomes INT_MIN, a negative. A negative value
will be returned for strings between INT_MAX and 2*INT_MAX characters in length.

This will occur on either a 64 bit environment (SIZE_MAX is 2**64-1, INT_MAX is
2**31-1) or a 32 bit environment (SIZE_MAX is 2**32-1, INT_MAX is 2**31-1).

Apologies, I have not built the latest libc to test, but see no evidence of this
behavior being catered for on inspecting the source of a checkout yesterday (14
Oct 2009).

How to reproduce (tested on 2.7 and 2.9):

#include <stdlib.h>
#include <limits.h>
#include <stdio.h>

/* Will need at least 2GB of RAM (or swap if you're patient). */

int main(void)
{
    size_t i, siz = INT_MAX+1LL;
    char *s = malloc(siz);
    if (!s) return 1;
    for (i = 0; i < siz; i++) /* fill it with non-'\0' printable garbage */
        s[i] = (i%10LL)+'0'; 
    s[siz-1] = '\0'; /* until the end */
    fprintf(stderr, "puts() returned %d\n", puts(s));
    return 0;
}

$ gcc test.c
$ ./a.out > /dev/null # discard the 2GB of "0123456789" output
puts() returned -2147483648

-- 
           Summary: puts(s) where strlen(s) >= INT_MAX returns a negative
                    value other than EOF
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: minor
          Priority: P2
         Component: libc
        AssignedTo: drepper at redhat dot com
        ReportedBy: chris-glibc at chris dot net dot au
                CC: glibc-bugs at sources dot redhat dot com
 GCC build triplet: x86_64-unknown-linux-gnu
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


http://sourceware.org/bugzilla/show_bug.cgi?id=10780

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug libc/10780] puts(s) where strlen(s) >= INT_MAX returns a negative value other than EOF
  2009-10-15  6:48 [Bug libc/10780] New: puts(s) where strlen(s) >= INT_MAX returns a negative value other than EOF chris-glibc at chris dot net dot au
@ 2009-10-29 18:23 ` drepper at redhat dot com
  2009-10-29 23:03 ` drepper at redhat dot com
  1 sibling, 0 replies; 3+ messages in thread
From: drepper at redhat dot com @ 2009-10-29 18:23 UTC (permalink / raw)
  To: glibc-bugs


------- Additional Comments From drepper at redhat dot com  2009-10-29 18:23 -------
You're right, there is a problem, but no guidance from the standard bodies as to
what the solution should be.  I've asked them.  Once I get a reply I'll change
the code.

-- 


http://sourceware.org/bugzilla/show_bug.cgi?id=10780

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug libc/10780] puts(s) where strlen(s) >= INT_MAX returns a negative value other than EOF
  2009-10-15  6:48 [Bug libc/10780] New: puts(s) where strlen(s) >= INT_MAX returns a negative value other than EOF chris-glibc at chris dot net dot au
  2009-10-29 18:23 ` [Bug libc/10780] " drepper at redhat dot com
@ 2009-10-29 23:03 ` drepper at redhat dot com
  1 sibling, 0 replies; 3+ messages in thread
From: drepper at redhat dot com @ 2009-10-29 23:03 UTC (permalink / raw)
  To: glibc-bugs


------- Additional Comments From drepper at redhat dot com  2009-10-29 23:03 -------
I've checked in a patch now.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


http://sourceware.org/bugzilla/show_bug.cgi?id=10780

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-10-29 23:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-15  6:48 [Bug libc/10780] New: puts(s) where strlen(s) >= INT_MAX returns a negative value other than EOF chris-glibc at chris dot net dot au
2009-10-29 18:23 ` [Bug libc/10780] " drepper at redhat dot com
2009-10-29 23:03 ` drepper at redhat dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).