LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 07-14-2015, 08:44 AM   #1
Ananda Bbau
LQ Newbie
 
Registered: May 2012
Posts: 9

Rep: Reputation: Disabled
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.

Last edited by Ananda Bbau; 07-14-2015 at 08:47 AM. Reason: Missed to mention clearly
 
Old 07-14-2015, 11:58 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,881
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.

Last edited by rtmistler; 07-14-2015 at 12:23 PM.
 
Old 07-14-2015, 12:20 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,790

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
actually you passed no args therefore some format string(s) may cause strange errors. you can try valgrind to catch that error
 
Old 07-14-2015, 12:22 PM   #4
Ananda Bbau
LQ Newbie
 
Registered: May 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-14-2015, 12:29 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,881
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Ananda Bbau View Post
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.
 
Old 07-14-2015, 12:31 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,790

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
% 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/*
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
vfprintf in 64-bit environment saurabhchokshi Programming 1 04-14-2010 02:01 PM
[video-editing] mencoder problem - segmentaion fault Orangutanklaus Linux - General 0 09-05-2007 03:36 PM
DirectFB Segmentaion Fault Greg Haynes Linux - Software 0 02-16-2005 10:59 PM
Segmentation fault when running ls kd7trn Linux - Software 2 09-10-2004 04:54 PM
segmentation fault running UT linoob Linux - Software 6 09-11-2003 08:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 04:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration