public inbox for glibc-bugs@sourceware.org help / color / mirror / Atom feed
From: "msebor at gmail dot com" <sourceware-bugzilla@sourceware.org> To: glibc-bugs@sourceware.org Subject: [Bug build/28439] New: -Wformat-overflow in resolv/res_query.c Date: Sat, 09 Oct 2021 17:55:28 +0000 [thread overview] Message-ID: <bug-28439-131@http.sourceware.org/bugzilla/> (raw) https://sourceware.org/bugzilla/show_bug.cgi?id=28439 Bug ID: 28439 Summary: -Wformat-overflow in resolv/res_query.c Product: glibc Version: unspecified Status: UNCONFIRMED Severity: normal Priority: P2 Component: build Assignee: unassigned at sourceware dot org Reporter: msebor at gmail dot com CC: carlos at redhat dot com Target Milestone: --- A GCC 12 enhancement (still under review) to the optimizer has improved GCC's ability to extract and propagate range information from strlen() expressions. The improvement lets GCC infer constraints on strlen() results from subsequent expressions involving lengths of different strings, like in the following code in resolv/res_query.c: n = strlen(name); d = strlen(domain); if (n + d + 1 >= MAXDNAME) { RES_SET_H_ERRNO(statp, NO_RECOVERY); return (-1); } sprintf (nbuf, "%s.%s",name, domain); Here, GCC determines from the if condition that n and d are each constrained to less than MAXDNAME, but because it can't express their relationship in the inequality, the constraint isn't tight enough to rule out that their sum might exceed MAXDNAME. This in turns triggers the warning below: res_query.c: In function ‘__res_context_querydomain’: res_query.c:613:35: warning: ‘%s’ directive writing up to 1023 bytes into a region of size between 1 and 1024 [-Wformat-overflow=] 613 | sprintf(nbuf, "%s.%s", name, domain); | ^~ res_query.c:613:17: note: ‘sprintf’ output between 2 and 2048 bytes into a destination of size 1025 613 | sprintf(nbuf, "%s.%s", name, domain); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This code triggered a similar false positive in the past. It was reported in GCC bug 91567 and quickly fixed there. In this case, however, a fix in GCC isn't feasible. Everything is working as designed, but the better range information exposed by the enhancement isn't sufficient to avoid the warning. The warning can be easily avoided in Glibc either by #pragma GCC diagnostic or by changing the code to avoid sprintf as shown below: index 75b0e5f2f7..31ab1db60b 100644 --- a/resolv/res_query.c +++ b/resolv/res_query.c @@ -610,7 +610,9 @@ __res_context_querydomain (struct resolv_context *ctx, RES_SET_H_ERRNO(statp, NO_RECOVERY); return (-1); } - sprintf(nbuf, "%s.%s", name, domain); + strcpy (nbuf, name); + nbuf[n] = '.'; + strcpy (nbuf + n + 1, domain); } return __res_context_query (ctx, longname, class, type, answer, anslen, answerp, answerp2, nanswerp2, -- You are receiving this mail because: You are on the CC list for the bug.
next reply other threads:[~2021-10-09 17:55 UTC|newest] Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-10-09 17:55 msebor at gmail dot com [this message] 2021-10-09 19:28 ` [Bug build/28439] " msebor at gmail dot com 2021-10-11 15:44 ` msebor 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-28439-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).