public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Yair Lenga <yair.lenga@gmail.com>
To: libc-help@sourceware.org
Subject: Buffer size checking for scanf* functions
Date: Tue, 5 Jul 2022 10:31:56 +0300	[thread overview]
Message-ID: <CAK3_KpM1i3=3PQ10ChixyZkdUJCBBUfN7B0A9VgsZ++grp2hjA@mail.gmail.com> (raw)

Hi,

Looking for feedback on the following age-old scanf problem: I'm trying to
perform a "safe' scanf, which will avoid buffer overflow.

#define XSIZE 30
char x[XSIZE] ;
sscanf(input, "%29s", x) ;

With the common pitfall that anytime the size of X is changed, the format
string MUST to be modified. One common approach, with glibc, is to use the
'm' modifier, switch x to char **x. However, this require code changes, and
is not practical with my existing code base.

My question: is there any extension to allow scanf to take an extra
argument (similar to the scanf_s proposal) - specifying the sizeof any
string arguments ?
sscanf(input, "%S", x, sizeof(x)) ;     // The 'S' require adding sizeof
parameter ?

If there is no such extension - how hard it will be to implement. I know
possible to define custom conversions for printf, I could not find anything
for scanf. IF this will be built into 'glibc', there IF it will be embedded
into the gcc format checks (2 big IFs), it can reduce the problems I (and I
believe many other developers) face when using those functions.

Yair

             reply	other threads:[~2022-07-05  7:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-05  7:31 Yair Lenga [this message]
2022-07-05 12:39 ` Adhemerval Zanella
2022-07-05 21:50 Yair Lenga
2022-07-06 12:10 ` Adhemerval Zanella
2022-07-06 15:04   ` Yair Lenga
2022-07-11 12:38     ` Adhemerval Zanella

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='CAK3_KpM1i3=3PQ10ChixyZkdUJCBBUfN7B0A9VgsZ++grp2hjA@mail.gmail.com' \
    --to=yair.lenga@gmail.com \
    --cc=libc-help@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: link
Be 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).