Segmentaion fault while running vfprintf()
Hi All,
While running the following program it is hitting segmentation fault. I am not able to find what is the issue there. 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 also but not able to find the problem (gdb) run Starting program: /ws/anaganes-sjc/junk warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000 Program received signal SIGSEGV, Segmentation fault. 0x0000003c44c7fb30 in wcslen () from /lib64/libc.so.6 (gdb) bt #0 0x0000003c44c7fb30 in wcslen () from /lib64/libc.so.6 #1 0x0000003c44c80b27 in wcsrtombs () from /lib64/libc.so.6 #2 0x0000003c44c464b2 in vfprintf () from /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/*") at junk.c:20 #4 0x0000000000400556 in main () at junk.c:9 (gdb) Could you please help me to solve this issue. I am not sure whether the problem is in the string passing.It is working for other string,even if the other string is more length than that above string. |
So here it is properly in [code] tags:
Code:
#include<stdio.h> Code:
$ cat file1.txt In running GDB, you should get a backtrace once you reach a segmentation fault, by using "bt". (Sorry, just noticing that you DID use the bt command :) Looks like the file lines don't match up what you labeled them as in your attempt to put it on the screen though...) It also says that the problem occurs at main.c:9 which is your fclose() statement, so perhaps put a breakpoint in there and determine if fp is NULL or invalid at that point. Maybe in your case, the file did not open due to permissions. Check that fp is non-NULL when you perform the fopen() call. |
actually you passed no args therefore some format string(s) may cause strange errors. you can try valgrind to catch that error
|
Hi rtmistler,
In that file stream (fp), whatever the strings in format that only will write file stream(fp). If you see the code, i am passing the line "/modules.php?name=Top&querylang=20WHERE201=2%20ALL%20SELECT%201,pwd,1,1%20FROM%20nuke_authors/*" in format. From the format it will write the stream into fp. But in compilation, you have got, "/modules.php?name=Top&querylang=20WHERE%201=2-0X1.5BE55B76CEFF4P-138LL " in file1.txt. Thanks for your reply. Thanks, G.Ananda Babu. |
Quote:
What I can say is that this is how I do something like this to create log files, a'la use of a macro statement: Code:
#define DBG_LOG "/home/user1/logs/dbg.log" |
% is a keyword for the format string and for example %20n would require an argument. You need to protect your % chars if you do not need that (that would be the doubled %)
Code:
/modules.php?name=Top&querylang=20WHERE201=220ALL20SELECT%%201,pwd,1,1%%20FROM%%20nuke_authors/* |
All times are GMT -5. The time now is 06:38 PM. |