newbie question seg fault printf

G

Greg

I am writing the basics of a cgi program in C. I am getting a wierd
segmentation fault with the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 80

/* function prototypes */
void parseParams (char *) ;
void parseQString (char *) ;
void unencode (char *, char *, char *) ;

int main (void) {

char *params = getenv ("QUERY_STRING") ;
char *paramCounter ;
char copy [MAXLEN] ;
int numParams = 0 ;

printf ("%s\n", params) ; // SEG FAULT HERE

// content header
printf("%s\n\n","Content-Type:text/plain");

parseQString (params) ;

return 0 ;
}

void parseQString (char *q_str) {
char *ptr = q_str ;

printf ("%s\n", ptr) ;

while (*ptr != '\0') {
printf ("%c\n", *ptr) ;
ptr++ ;
}
}

When I try to print out the contents of params in the main program, I
get a segmentation fault. However, when I print out the contents in
the parseQString function, there is no problem at all. Does anyone
have any idea why it would give me an error in one place but not the
other?

Thanks

Greg
 
R

Régis Troadec

Greg said:
I am writing the basics of a cgi program in C. I am getting a wierd
segmentation fault with the following code:
Hi,


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 80

/* function prototypes */
void parseParams (char *) ;
void parseQString (char *) ;
void unencode (char *, char *, char *) ;

int main (void) {

char *params = getenv ("QUERY_STRING") ;

You don't test if params is NULL or not. If it's NULL, that leads to a
potential segfault if you use it afterwards in your program, like with
printf.

if (params == NULL)
{
fprintf(stderr, "getenv query failed\n");
/*exit(EXIT_FAILURE);*/
/* or */
params = "getenv query failed";
}

It looks like QUERY_STRING doesn't exist.
char *paramCounter ;
char copy [MAXLEN] ;
int numParams = 0 ;

printf ("%s\n", params) ; // SEG FAULT HERE

params should be NULL, as said above, and then you obtain a segmentation
fault. Testing params before displaying it is another possible solution
here.
if (param != NULL)
{
printf ("%s\n", params) ;
}
But i'ts not my favourite solution, since it doesn't prevent any other
access to params in your program and you might test params each time it
appears, like before calling parseQString for example.

// content header
printf("%s\n\n","Content-Type:text/plain");

parseQString (params) ;

return 0 ;
}

void parseQString (char *q_str) {
char *ptr = q_str ;

printf ("%s\n", ptr) ;

while (*ptr != '\0') {
printf ("%c\n", *ptr) ;
ptr++ ;
}
}

When I try to print out the contents of params in the main program, I
get a segmentation fault. However, when I print out the contents in
the parseQString function, there is no problem at all. Does anyone
have any idea why it would give me an error in one place but not the
other?

Thanks

Greg

Regis
 
M

Martin Ambuhl

Greg said:
I am writing the basics of a cgi program in C. I am getting a wierd
segmentation fault with the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 80

/* function prototypes */
void parseParams (char *) ;
void parseQString (char *) ;
void unencode (char *, char *, char *) ;

int main (void) {

char *params = getenv ("QUERY_STRING") ;
char *paramCounter ;
char copy [MAXLEN] ;
int numParams = 0 ;

printf ("%s\n", params) ; // SEG FAULT HERE

getenv returns NULL if the argument does not exist in the envirinment.
You do not check to see if params is NULL before you attempt to use it.

Try the following with the deadwood cut out but with a check for NULL
built in:

#include <stdio.h>
#include <stdlib.h>

void parseQString(char *);

int main(void)
{
char *params = getenv("QUERY_STRING");
printf("%s\n", params ? params : "(null)");
parseQString(params);
return 0;
}

void parseQString(char *q_str)
{
char *ptr = q_str;
printf("%s\n", ptr ? ptr : "(null)");
}
 
A

Al Bowers

Martin said:
Greg said:
I am writing the basics of a cgi program in C. I am getting a wierd
segmentation fault with the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 80

/* function prototypes */
void parseParams (char *) ;
void parseQString (char *) ;
void unencode (char *, char *, char *) ;

int main (void) {

char *params = getenv ("QUERY_STRING") ;
char *paramCounter ;
char copy [MAXLEN] ;
int numParams = 0 ;
printf ("%s\n", params) ; // SEG FAULT HERE


getenv returns NULL if the argument does not exist in the envirinment.
You do not check to see if params is NULL before you attempt to use it.

Try the following with the deadwood cut out but with a check for NULL
built in:

#include <stdio.h>
#include <stdlib.h>

void parseQString(char *);

int main(void)
{
char *params = getenv("QUERY_STRING");
printf("%s\n", params ? params : "(null)");
parseQString(params);
return 0;
}

void parseQString(char *q_str)

If the value of function getenv is not a nul pointer, it
points to a string. The string pointed to cannot be
modified by the program. Using the value returned by getenv
as an argument in a function of name parseQString makes me
nervous. I would use a const char * type instead
of a char * in the code.
 
B

Barry Schwarz

I am writing the basics of a cgi program in C. I am getting a wierd
segmentation fault with the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 80

/* function prototypes */
void parseParams (char *) ;
void parseQString (char *) ;
void unencode (char *, char *, char *) ;

int main (void) {

char *params = getenv ("QUERY_STRING") ;
char *paramCounter ;
char copy [MAXLEN] ;
int numParams = 0 ;

printf ("%s\n", params) ; // SEG FAULT HERE

getenv can return NULL. You need to insure it did not before you
attempt to dereference params.


<<Remove the del for email>>
 
G

Greg

OK I realized what it was like 5 mins after I put up the original
post. It ended up not being a seg fault at all - I was just trying to
send output before the content header.

I had:
printf ("%s\n", params) ; // SEG FAULT HERE

// content header
printf("%s\n\n","Content-Type:text/plain");

rather than:

printf("%s\n\n","Content-Type:text/plain");
printf ("%s\n", params) ;

Which ended up fixing it.

Thanks for all the quick responses people!

-G


I am writing the basics of a cgi program in C. I am getting a wierd
segmentation fault with the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 80

/* function prototypes */
void parseParams (char *) ;
void parseQString (char *) ;
void unencode (char *, char *, char *) ;

int main (void) {

char *params = getenv ("QUERY_STRING") ;
char *paramCounter ;
char copy [MAXLEN] ;
int numParams = 0 ;

printf ("%s\n", params) ; // SEG FAULT HERE

// content header
printf("%s\n\n","Content-Type:text/plain");

parseQString (params) ;

return 0 ;
}

void parseQString (char *q_str) {
char *ptr = q_str ;

printf ("%s\n", ptr) ;

while (*ptr != '\0') {
printf ("%c\n", *ptr) ;
ptr++ ;
}
}

When I try to print out the contents of params in the main program, I
get a segmentation fault. However, when I print out the contents in
the parseQString function, there is no problem at all. Does anyone
have any idea why it would give me an error in one place but not the
other?

Thanks

Greg
 
M

Martin Ambuhl

Greg said:
OK I realized what it was like 5 mins after I put up the original
post. It ended up not being a seg fault at all - I was just trying to
send output before the content header.

I had:




rather than:





Which ended up fixing it.

Thanks for all the quick responses people!

You _still_ need to add a check for params==NULL. Just because your
program works at the moment is no excuse.
 
B

Barry Schwarz

OK I realized what it was like 5 mins after I put up the original
post. It ended up not being a seg fault at all - I was just trying to
send output before the content header.

I had:


rather than:



Which ended up fixing it.
I give up. How does reversing the order of the calls to printf solve
the problem? What does the second call print?


<<Remove the del for email>>
 
G

Greg

I give up. How does reversing the order of the calls to printf solve
the problem? What does the second call print?

All of the output is being sent to a webpage, so it needed to know
what type of output to expect. When the content header is sent first,
the webpage knows that it will be expecting plain text. When I tried
to send output before the content header it returned an error, which I
wrongly assumed was a seg fault.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top