Segmentation fault -- what's that?

I'm a student. Our final class project is the department website. My job is working on the Company Database, a db of company info and contacts that a professor has kept, and students use for job searches, etc. The server is Apache under Linux, a MySQL database, CGI's in C. We want to make it available on the website, registered users of course. I've got a certain amount of the HTML and CGI's working, and I'm pretty sure the connection and query code is OK here. But I've had problems with string functions to handle the result set from the queries, and in other CGI's I've had to avoid strcat. This one I decided to play with in the console, since compiling and running in the browser is such a pain. Right now I just want to test the idea of building a list of all the cities where there are companies in the db. If I query the db for all the cities in the city field, I get every instance of every city. I can get a list in the MySQL server itself, alphabetically, so Boston is listed a bunch of times. I figure I can do a while loop and use a temp string to see if the town has been added to the final output list yet. If not, it goes to printf("%s",row[0]) and thus out to the HTML building the list for display.

Sounds good, but I get



Segmentation fault (core dumped)

In other CGI's, I would get "Internal Server Error" messages at times. Apparently either Apache or MySQL itself is choking on the code.

Help?

#include
#include
#include
#include "uncgi.h"

int main()
{
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL_FIELD *field;
MYSQL_FIELD *fields;
MYSQL *connection;
MYSQL mysql;

int state;
int i;

char select_string[90]="select CPET2.Town from CPET2 order by Town;";
char * temp = NULL;
char * temprow;

// uncgi();

printf("Content-type: text/html


"); // begin HTML
printf("
");
printf("
");

mysql_init(&mysql);

connection=mysql_real_connect(&mysql,"desktop","monty","full","CPETDB",0,"socketfilename",0);
if (connection == NULL)
{
printf(mysql_error(&mysql)) ;
return 1 ;
}

state = mysql_query(connection,select_string);
if (state != 0)
{
printf(mysql_error(connection));
return 1;
printf("state!=0");
}

printf("");
printf("");


result = mysql_store_result(connection);
while ((row = mysql_fetch_row(result)) != NULL);
{
temprow = row[0];

if (strcmp(temp,temprow))
{
temp = row[0];
printf("%s",row[0]);
}
}

printf("");
printf("");
printf("");

mysql_free_result(result);
mysql_close(connection);


return 0;
} // end main

I left the HTML stuff in, figuring it shouldn't cause a problem just getting printed to the screen.


Comments

  • : [...]
    : Sounds good, but I get
    :
    :
    :
    : Segmentation fault (core dumped)
    :
    : In other CGI's, I would get "Internal Server Error" messages at times. Apparently either Apache or MySQL itself is choking on the code.
    :
    : Help?

    Just looking it over quickly, I see something that could definitely cause a segmentation fault. Look at the line I highlighted in red below. You're passing NULL to a strcmp(). Not good.

    BTW: This would have been pretty easy to catch if you had stepped through this in a debugger.

    Cheers,
    Eric

    [code]#include
    #include
    #include
    #include "uncgi.h"

    int main()
    {
    MYSQL_RES *result;
    MYSQL_ROW row;
    MYSQL_FIELD *field;
    MYSQL_FIELD *fields;
    MYSQL *connection;
    MYSQL mysql;

    int state;
    int i;

    char select_string[90]="select CPET2.Town from CPET2 order by Town;";
    char * temp = NULL;
    char * temprow;

    // uncgi();

    printf("Content-type: text/html


    "); // begin HTML
    printf("
    ");
    printf("
    ");

    mysql_init(&mysql);

    connection=mysql_real_connect(&mysql,"desktop","monty","full","CPETDB",0,"socketfilename",0);
    if (connection == NULL)
    {
    printf(mysql_error(&mysql)) ;
    return 1 ;
    }

    state = mysql_query(connection,select_string);
    if (state != 0)
    {
    printf(mysql_error(connection));
    return 1;
    printf("state!=0");
    }

    printf("");
    printf("");


    result = mysql_store_result(connection);
    while ((row = mysql_fetch_row(result)) != NULL);
    {
    temprow = row[0];

    [red]if (strcmp(temp,temprow))[/red]
    {
    temp = row[0];
    printf("%s",row[0]);
    }
    }

    printf("");
    printf("");
    printf("");

    mysql_free_result(result);
    mysql_close(connection);


    return 0;
    } // end main[/code]




  • : Just looking it over quickly, I see something that could definitely cause a segmentation fault. Look at the line I highlighted in red below. You're passing NULL to a strcmp(). Not good.
    :
    : BTW: This would have been pretty easy to catch if you had stepped through this in a debugger.
    :
    : Cheers,
    : Eric
    :

    Eric --

    Duh ... I see it. I hope it will work to initialize

    char * temp = ""; instead?

    This is all being done in raw C under RedHat Linux, in the terminal emulation window. A valuable experience, but tough.

    A simple explanation of segmentation fault?

    thanks

    F

  • :
    : : Just looking it over quickly, I see something that could definitely cause a segmentation fault. Look at the line I highlighted in red below. You're passing NULL to a strcmp(). Not good.
    : :
    : : BTW: This would have been pretty easy to catch if you had stepped through this in a debugger.
    : :
    : : Cheers,
    : : Eric
    : :
    :
    : Eric --
    :
    : Duh ... I see it. I hope it will work to initialize
    :
    : char * temp = ""; instead?
    :
    : This is all being done in raw C under RedHat Linux, in the terminal emulation window. A valuable experience, but tough.
    :
    : A simple explanation of segmentation fault?
    :
    : thanks
    :
    : F
    :

    I believe a seg fault is one which involes attempted access to protected memory. Each process is allocated specified pages of memory which it can use. In decent operating systems (pretty much anything other than DOS), processes cannot access the memory pages that are used by other processes so as to avoid corruption. If you try to access invalid memory (eg by addressing memory address 0000:0000 (NULL)) you'll get a seg fault :)


  • MatosMatos Mexico

    Hi, where i found the "uncgi.h"??

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories