Skip to content

Instantly share code, notes, and snippets.

@if1live
Created March 6, 2016 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save if1live/30e589d78ecd11406b2d to your computer and use it in GitHub Desktop.
Save if1live/30e589d78ecd11406b2d to your computer and use it in GitHub Desktop.
printf(%s) with null pointer
/*
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