public inbox for glibc-bugs@sourceware.org help / color / mirror / Atom feed
From: "i at maskray dot me" <sourceware-bugzilla@sourceware.org> To: glibc-bugs@sourceware.org Subject: [Bug stdio/31251] New: libio: snprintf(str, SIZE_MAX, "Hello world!") does not write the last char Date: Tue, 16 Jan 2024 23:21:53 +0000 [thread overview] Message-ID: <bug-31251-131@http.sourceware.org/bugzilla/> (raw) https://sourceware.org/bugzilla/show_bug.cgi?id=31251 Bug ID: 31251 Summary: libio: snprintf(str, SIZE_MAX, "Hello world!") does not write the last char Product: glibc Version: 2.37 Status: NEW Severity: normal Priority: P2 Component: stdio Assignee: unassigned at sourceware dot org Reporter: i at maskray dot me Target Milestone: --- Since about https://sourceware.org/git/?p=glibc.git;a=commit;h=e88b9f0e5cc50cab57a299dc7efe1a4eb385161d (stdio-common: Convert vfprintf and related functions to buffers) ``` % cat sn.c #include <stdio.h> #include <stdint.h> int main() { char str[20]; snprintf(str, SIZE_MAX, "Hello world!"); puts(str); } % cc sn.c % ./a.out Hello world ``` Call trace: __vsnprintf_internal __printf_buffer_snprintf_init __printf_buffer_init __printf_buffer_init_end: `base + len` (write_end) may wrap around if `len` is too large (e.g. SIZE_MAX) __printf_buffer_snprintf_done The condition `if (buf->base.write_ptr < buf->base.write_end)` fails. `buf->base.write_ptr[-1] = '\0' drops the last char. Notes: Bounds-checked functions, such as printf_s , fprintf_s, sprintf_s, and snprintf_s, specify RSIZE_MAX as the upperbound. We don't define RSIZE_MAX. musl snprintf returns -1 with errno=EOVERFLOW when bufsz > INT_MAX. % cat sn.c #include <stdio.h> #include <limits.h> int main() { char str[20]; int ret = snprintf(str, INT_MAX, "a!"); printf("%d %s\n", ret, str); ret = snprintf(str, INT_MAX+1L, "a!"); printf("%d %s\n", ret, str); } % ~/musl/out/debug/obj/musl-gcc sn.c -o sn.musl && ./sn.musl 2 a! -1 a! -- You are receiving this mail because: You are on the CC list for the bug.
next reply other threads:[~2024-01-16 23:21 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2024-01-16 23:21 i at maskray dot me [this message] 2024-01-16 23:23 ` [Bug stdio/31251] " i at maskray dot me 2024-01-17 9:32 ` schwab@linux-m68k.org 2024-01-26 1:47 ` gabravier at gmail dot com
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-31251-131@http.sourceware.org/bugzilla/ \ --to=sourceware-bugzilla@sourceware.org \ --cc=glibc-bugs@sourceware.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).