Difference between% ms and% s scanf

Reading the manual scanfI come across this line:

Optional character "m". This is used to convert strings (% s,% c,% [),

Can someone explain this with a simple example, indicating the difference and the need for such an option in some cases?

+4
source share
1 answer

The C standard does not define such an optional character in formats scanf().

GNU lib C thus defines an optional indicator a(from the man page for scanf):

a. : scanf() , char * ( ).

free , . GNU; C99 a ( GNU).

:

a , gcc -std=c99 gcc -D_ISOC99_SOURCE ( _GNU_SOURCE), a (. ).

2.7, glibc m , . m :

  • %c (, %3mc).

  • %a ( gcc -std=c99 ..)

  • POSIX.1.

-linux http://linux.die.net/man/3/scanf :

"m". (%s, %c, %[), : , scanf() , , char * ( ). free(3) , .

Posix POSIX.1-2008 (. http://pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html):

%c, %s %[ m, , . , , -, . , malloc(). . , errno [ENOMEM], . EOF, , m , .

, :

char *p;
scanf("%ms", &p);

scanf '\0'. p, scanf() 1, stdin.

, m - . , , , .

, scanf():

'\0':

    char buffer[20];
    scanf("%19s", buffer);

, . , , undefined, , :

    char buffer[20];
    scanf("%s", buffer); // potential undefined behavior,
                         // that could be exploited by an attacker.
+7

Source: https://habr.com/ru/post/1649697/


All Articles