c++ - Segmentation fault while using vfprintf() -


the following program hitting segmentation fault, , cannot figure out problem is.

  1 #include<stdio.h>   2 #include<stdarg.h>   3 void writeformat(file*,char*, ...);   4 int main()   5 {   6   file *fp;   7   fp=fopen("file1.txt","w");   8   writeformat(fp,"/modules.php?name=top&querylang=%20where%201=2%20all%20select%201,pwd,1,1%20from%20nuke_authors/*");   9   fclose(fp);  10   return(0);  11 }  12   13 void writeformat(file *stream,char *format, ...)  14 {  15   va_list args;  16   va_start(args,format);  17   vfprintf(stream,format,args);  18   va_end(args);  19 } 

i tried in gdb, , tells me problem in vfprintf():

(gdb) run starting program: /ws/anaganes-sjc/junk  warning: no loadable sections found in added symbol-file system-supplied dso @ 0x2aaaaaaab000  program received signal sigsegv, segmentation fault. 0x0000003c44c7fb30 in wcslen () /lib64/libc.so.6 (gdb) bt #0  0x0000003c44c7fb30 in wcslen () /lib64/libc.so.6 #1  0x0000003c44c80b27 in wcsrtombs () /lib64/libc.so.6 #2  0x0000003c44c464b2 in vfprintf () /lib64/libc.so.6 #3  0x0000000000400649 in writeformat (stream=0x601010, format=0x400758 "/modules.php?name=top&querylang=%20where%201=2%20all%20select%201,pwd,1,1%20from%20nuke_authors/*") @ junk.c:20 #4  0x0000000000400556 in main () @ junk.c:9 

can please me find problem?

your format string contains escaped space characters. escape done percent signs, html style:

"querylang=%20where%201=2%20all%20select%201..." 

these percent signs have meaning in printf style format strings. must either render spaces verbatim:

"querylang= 1=2 select 1..." 

or use printf's own escape printing percent signs, namely %%:

"querylang=%%20where%%201=2%%20all%%20select%%201..." 

or, alk points out in comment, use string format , print string argument:

writeformat(fp, "%s", "/modules.php?name="); 

which best way print strings have or have formatting specifiers verbatim.

you segmentation violation, because each format specified % except %% expects additional argument. example %20a withh print binary representation of float of width 20. therefore expects double argument, haven't specified arguments, vprintf tries acess memory beyond bound of variable argument list.

many compilers can warn format mismatches well-known printf functions. compilers allow label arguments of own functions printf format strings. microsoft's sal or gcc-style attributes let that.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -