Created
March 6, 2016 06:00
-
-
Save if1live/30e589d78ecd11406b2d to your computer and use it in GitHub Desktop.
printf(%s) with null pointer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
printf(%s) with null pointer | |
## Execute | |
$ clang test_format_null.c && ./a.out | |
## Output | |
### OSX | |
|(null)||(nu| | |
### Linux with glibc | |
|(null)||| | |
*/ | |
#include <stdio.h> | |
int main() | |
{ | |
printf("|%s|%.0s|%.3s|\n", NULL, NULL, NULL); | |
return 0; | |
} | |
/* | |
# Related source | |
glibc, stdio-common/vfprintf.c | |
https://sourceware.org/git/?p=glibc.git;a=blob;f=stdio-common/vfprintf.c;h=6829d4dc8e7fe7c066a06f1857ee926e0f48c379;hb=HEAD#l989 | |
*/ | |
// # How it works? | |
// ## glibc | |
// 1. REF (form_string), /* for 's', 'S' */ | |
// 2. REF (precision), /* for '.' */ | |
// 3. LABEL (form_string): | |
// if (string == NULL) | |
// { | |
// /* Write "(null)" if there's space. */ | |
// if (prec == -1 || prec >= (int) sizeof (null) - 1) | |
// { | |
// string = (char *) null; | |
// len = sizeof (null) - 1; | |
// } | |
// else | |
// { | |
// string = (char *) ""; | |
// len = 0; | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment