This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug libc/10780] New: puts(s) where strlen(s) >= INT_MAX returns a negative value other than EOF
- From: "chris-glibc at chris dot net dot au" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sources dot redhat dot com
- Date: 15 Oct 2009 06:48:03 -0000
- Subject: [Bug libc/10780] New: puts(s) where strlen(s) >= INT_MAX returns a negative value other than EOF
- Reply-to: sourceware-bugzilla at sourceware dot org
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.