c - warning: format string contains '\0' within the string body [-Wformat] -
i've got system() command inside code uses awk. can't figure out how fix issue with\x00 hexadecimal values. apparently need terminated differently, that's beyond realm of know.
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { char command[128]; snprintf(command, sizeof(command), "awk '{ gsub (/\xab\x00\x00\xbc/,\"\xbc\x00\x00\xab\") ; print }' %s", argv[1]); system(command); } warnings/errors:
> test.c:8:56: warning: format string contains '\0' within string body [-wformat] > snprintf(command, sizeof(command), "awk '{ gsub (/\xab\x00\xbc/,\"\xbc\x00\x00\xab\") ; print }' %s", argv[1]); > /usr/include/secure/_stdio.h:57:62: note: expanded macro 'snprintf' > __builtin___snprintf_chk (str, len, 0, __darwin_obsz(str), __va_args__) ^ 1 warning generated. sh: -c: line 0: unexpected eof while looking matching `'' sh: -c: line 1: syntax error: unexpected end of file sorry if has been asked before, couldn't find relevant info though in search on how fix this, thanks...
consider c string literal "\xab". string literal contains 1 byte, not 4. similarly, "\x00" string literal contains 1 byte null byte. clang warns because null byte ends c string — every character after ignored library functions such snprintf.
in awk code, there's awk string literal, surrounded double quotes. wrote …\"\xbc\x00\x00\xab\"…, backslashes in front of double quotes, because otherwise double quotes interpreted ending c string literal. similarly, if want end backslash in awk code (more precisely, in shell command), need backslash in front of it. in other words, need double backslashes.
snprintf(command, sizeof(command), "awk '{ gsub (/\\xab\\x00\\x00\\xbc/,\"\\xbc\\x00\\x00\\xab\") ; print }' %s", argv[1]); beware there's quoting problem program: interprets argument snippet of shell code, not file name. 2 coincide if file name doesn't contain shell special characters. example, ./your_program jack.txt work, not ./your_program "o'leary.txt". make work, need massage argument protect shell special characters.
(another problem don't check whether snprintf succeeds. overflow — you should dynamically allocate necessary size based on length of argument (don't forget account quoting if argument contains special characters).)
Comments
Post a Comment