diff options
| author | Marshel Abraham <Marshel.Abraham@in.bosch.com> | 2013-02-28 14:18:09 +0100 |
|---|---|---|
| committer | Michael Kerrisk <mtk.manpages@gmail.com> | 2013-02-28 15:00:09 +0100 |
| commit | 753f3bace98aebedf15355e68505b13b9708f8d0 (patch) | |
| tree | 51a2453aea896d5da9ba0fbabd84c36b83d20c35 /man3/printf.3 | |
| parent | e0a699c1eb7e2d4d3bb13a6ff14fe4adb68fb650 (diff) | |
| download | man-pages-753f3bace98aebedf15355e68505b13b9708f8d0.tar.gz | |
printf.3: Fix error handling in example code
See https://bugzilla.kernel.org/show_bug.cgi?id=23282
Reported-by: Graham Gower <graham.gower@gmail.com>
Acked-by: Graham Gower <graham.gower@gmail.com>
Signed-off-by: Marshel Abraham <Marshel.Abraham@in.bosch.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
Diffstat (limited to 'man3/printf.3')
| -rw-r--r-- | man3/printf.3 | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/man3/printf.3 b/man3/printf.3 index bbb2f5b879..09f8e78b68 100644 --- a/man3/printf.3 +++ b/man3/printf.3 @@ -1025,6 +1025,9 @@ one might obtain "Sonntag, 3. Juli, 10:02". .PP To allocate a sufficiently large string and print into it (code correct for both glibc 2.0 and glibc 2.1): +.PP +If truncation occurs in glibc versions prior to 2.0.6, this is treated as an +error instead of being handled gracefully. .nf #include <stdio.h> @@ -1050,17 +1053,21 @@ make_message(const char *fmt, ...) n = vsnprintf(p, size, fmt, ap); va_end(ap); + /* Check error code */ + + if (n < 0) + return NULL; + /* If that worked, return the string. */ - if (n > \-1 && n < size) + if (n < size) return p; /* Else try again with more space. */ - if (n > \-1) /* glibc 2.1 */ + else size = n+1; /* precisely what is needed */ - else /* glibc 2.0 */ - size *= 2; /* twice the old size */ + if ((np = realloc (p, size)) == NULL) { free(p); |
