Skip to content

Instantly share code, notes, and snippets.

@Keith-S-Thompson
Last active January 6, 2017 23:20
Show Gist options
  • Save Keith-S-Thompson/ec46b5dc43a2f5de5881b8fede050b25 to your computer and use it in GitHub Desktop.
Save Keith-S-Thompson/ec46b5dc43a2f5de5881b8fede050b25 to your computer and use it in GitHub Desktop.

Demo program for a bug that appears under Termux on Android.

The %n format for printf does not work.

Files:

  • percent-n-test.c is the test program.
  • correct-output.txt is the correct output
  • incorrect-output.txt is the output under Termux on my Nexus 9, Android 7.1.1

UPDATE : The problem is that the C runtime library used by Android, bionic, deliberately disables the %n format specifier for *print(), as explained here. (There are some known security problems with %n, but this means the C implementation is non-conforming.)

Hello, world
a=0 b=5 c=13
PASSED
nHellon, world
na=100 b=200 c=300
FAILED, variables unchanged
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a = 100, b = 200, c = 300;
printf("%nHello%n, world\n%n", &a, &b, &c);
printf("a=%d b=%d c=%d\n", a, b, c);
if (a == 0 && b == 5 && c == 13) {
puts("PASSED");
exit(EXIT_SUCCESS);
}
else if (a == 100 && b == 200 && c == 300) {
puts("FAILED, variables unchanged\n");
exit(EXIT_FAILURE);
}
else {
puts("FAILED, variables changed but incorrect\n");
exit(EXIT_FAILURE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment