Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 416315 - sys-libs/glibc [s]scanf : "%u" format specifier should not accept negative integers
Summary: sys-libs/glibc [s]scanf : "%u" format specifier should not accept negative in...
Status: RESOLVED UPSTREAM
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Core system (show other bugs)
Hardware: x86 Linux
: Normal minor (vote)
Assignee: Gentoo Toolchain Maintainers
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-05-16 19:11 UTC by Christopher Friedt
Modified: 2012-05-16 22:03 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christopher Friedt 2012-05-16 19:11:01 UTC
[s]scanf should fail when scanning any negative integer (i.e. a natural number preceded by the '-' sign) if the "%u" format specifier is given.

test:

#include <stdint.h>
#include <stdio.h>

int main() {
    int r;
    const char *x = "-1";
    uint32_t y;
    if ( 1 == sscanf(x,"%u",&y) ) {
        printf("sscanf incorrectly scanned a signed integer using an "
               "unsigned format specifier resulting in y=%u\n", y);
        r = -1;
    } else {
        printf("sscanf correctly failed to scan a signed integer "
               "using an unsigned format specifier\n");
        r = 0;
    }
    return r;
}

Quoting from "man (3) scanf"

"u      Matches  an unsigned decimal integer"


Reproducible: Always

Steps to Reproduce:
1. run test
2.
3.
Actual Results:  
sscanf incorrectly scanned a signed integer using an unsigned format specifier resulting in y=4294967295

Expected Results:  
sscanf correctly failed to scan a signed integer using an unsigned format specifier

Can't say at the moment whether this is part of iso/c99 or ansi, but it really should be.
Comment 1 SpanKY gentoo-dev 2012-05-16 22:03:27 UTC
seems to me it's acting correctly according to POSIX.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html

 u
  Matches an optionally signed decimal integer, whose format is the same as
  expected for the subject sequence of strtoul() with the value 10 for the
  base argument. In the absence of a size modifier, the application shall
  ensure that the corresponding argument is a pointer to unsigned.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoul.html

  The subject sequence is defined as the longest initial subsequence of the
  input string, starting with the first non-white-space character that is of
  the expected form. The subject sequence shall contain no characters if the
  input string is empty or consists entirely of white-space characters, or
  if the first non-white-space character is other than a sign or a
  permissible letter or digit.

if you want to dispute that, feel free to bring it to the upstream mailing list and/or bugzilla as i don't plan on doing anything unique here ...