Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 23 Mar 2012 19:06:38 GMT
From:      Nathan Weeks <weeks@iastate.edu>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   standards/166349: Support the assignment-allocation character for fscanf()/scanf()/sscanf()
Message-ID:  <201203231906.q2NJ6cs5040371@red.freebsd.org>
Resent-Message-ID: <201203231910.q2NJADKM029558@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         166349
>Category:       standards
>Synopsis:       Support the assignment-allocation character for fscanf()/scanf()/sscanf()
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-standards
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri Mar 23 19:10:13 UTC 2012
>Closed-Date:
>Last-Modified:
>Originator:     Nathan Weeks
>Release:        9.0
>Organization:
USDA-ARS
>Environment:
FreeBSD  9.0-RELEASE FreeBSD 9.0-RELEASE #0: Tue Jan  3 07:46:30 UTC 2012     root@farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  amd64
>Description:
In POSIX.1-2008, the %c , %s , and %[ conversion specifiers in a
fscanf()/scanf()/sscanf() format string accept an optional
assignment-allocation character 'm' to allocate a memory buffer large enough
to hold the converted string:

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

This functionality is useful for safely reading formatted input strings of unknown size.

The FreeBSD 9.0 libc does not support this feature.

In addition, the gcc 4.6.3 port silently accepts the assignment-allocation character, even when "-Wall" is specified, but a memory buffer is apparently not allocated at run time.
>How-To-Repeat:
Compile the attached program (scanf_m.c) using the base gcc 4.2.1:

#####################################
$ gcc -Wall scanf_m.c
scanf_m.c: In function 'main':
scanf_m.c:6: warning: unknown conversion type character 'm' in format
scanf_m.c:6: warning: too many arguments for format
$ echo test | ./a.out
H??
Segmentation fault (core dumped)
#####################################

Using the gcc 4.6.3 port:
#####################################
$ gcc46 -Wall scanf_m.c
$ echo test | ./a.out
H??
Segmentation fault (core dumped)
#####################################
 

>Fix:


Patch attached with submission follows:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char *strptr;
    if (scanf("%m[^\n]\n", &strptr) == EOF) {
        perror("scanf");
        return 1;
    }
    printf("%s\n", strptr);

    free(strptr);

    return 0;
}


>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201203231906.q2NJ6cs5040371>