LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Red Hat (https://www.linuxquestions.org/questions/red-hat-31/)
-   -   Segmentaion fault while running vfprintf() (https://www.linuxquestions.org/questions/red-hat-31/segmentaion-fault-while-running-vfprintf-4175547950/)

Ananda Bbau 07-14-2015 08:44 AM

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.

rtmistler 07-14-2015 11:58 AM

So here it is properly in [code] tags:
Code:

#include<stdio.h>
#include<stdarg.h>

void writeformat(FILE*,char*, ...);

int main()
{
    FILE *fp;
    fp=fopen("file1.txt","w");
    writeformat(fp,"/modules.php?name=Top&querylang=20WHERE201=220ALL20SELECT%201,pwd,1,1%20FROM%20nuke_authors/*");
    fclose(fp);
    return(0);
}

void writeformat(FILE *stream,char *format, ...)
{
    va_list args;
    va_start(args,format);
    vfprintf(stream,format,args);
    va_end(args);
}

I compiled it, and it ran perfectly fine.
Code:

$ cat file1.txt
/modules.php?name=Top&querylang=20WHERE%201=2-0X1.5BE55B76CEFF4P-138LL

When you compile it for gdb, did you use the -ggdb flag?

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.

pan64 07-14-2015 12:20 PM

actually you passed no args therefore some format string(s) may cause strange errors. you can try valgrind to catch that error

Ananda Bbau 07-14-2015 12:22 PM

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.

rtmistler 07-14-2015 12:29 PM

Quote:

Originally Posted by Ananda Bbau (Post 5391325)
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.

Firstly, quite right. How about you start with a less complicated print string by the way just to validate that you don't get errors.

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"
#define dbg_log(format, ...) { dbgLog = fopen(DBG_LOG, "a"); if(dbgLog != NULL) { fprintf(dbgLog, format, ##__VA_ARGS__); fclose(dbgLog); } }

And in the main source file I also have to have a FILE *dbgLog; statement to declare that pointer.

pan64 07-14-2015 12:31 PM

% 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.