A
A
Azat Seidakhmet2017-06-02 15:52:04
linux
Azat Seidakhmet, 2017-06-02 15:52:04

Segmentation fault (memory dumped) in C and Ubuntu?

Hello!
I don't understand what is causing the error. At the first iteration, it makes a record in the database, but then an error immediately occurs. Googled about leaks and appeals to a foreign segment, but didn’t really understand anything. Therefore, I ask you to poke your nose into a bowl. Where and what needs to be fixed?
Here is the actual code:

#include <my_global.h>
#include <mysql.h>
#include <string.h>

void puterror(char *);
void getDesc(char *);
void insertLang(char *);

int main(int argc, char *argv[])
{
  MYSQL conn;
  MYSQL_RES *res;
  MYSQL_ROW row;

  if(!mysql_init(&conn))
     puterror("Error: can't create MySQL-descriptor\n");

  if(!mysql_real_connect(&conn, "localhost", "root", "password", "Almata", 0, NULL,0))
     puterror("Error: can't connect to MySQL server\n");

  if(mysql_query(&conn, "SET NAMES 'utf8'") != 0)
     puterror("Error: can't set character set\n");

  if(mysql_query(&conn, "SELECT * FROM lib_book") != 0)
     puterror("Error: can't execute SQL-query\n");

  res = mysql_store_result(&conn);
  if(res == NULL) puterror("Error: can't get the result description\n");

  if(mysql_num_rows(res) > 0)
  {
    while((row = mysql_fetch_row(res)) != NULL)
    {
      getDesc(row[36]);
    }
  }

  mysql_free_result(res);
  mysql_close(&conn);
  
}


void getDesc(char * book_id){
  char * queryStr = "SELECT * FROM tbl_BookDesc WHERE ID_Book=";
  char * new_str;
  if((new_str = malloc(strlen(queryStr)+strlen(book_id)+1)) != NULL){
      new_str[0] = '\0';   // ensures the memory is an empty string
      strcat(new_str,queryStr);
      strcat(new_str,book_id);
  } else {
      printf("malloc failed!\n");
      // exit?
  }
  MYSQL conn1;
  MYSQL_RES *res1;
  MYSQL_ROW row1;
  char * insQuery = "";
  if(!mysql_init(&conn1))
     puterror("Error: can't create MySQL-descriptor\n");

  if(!mysql_real_connect(&conn1, "localhost", "root", "password", "books", 0, NULL,0))
     puterror("Error: can't connect to MySQL server\n");

  if(mysql_query(&conn1, "SET NAMES 'utf8'") != 0)
     puterror("Error: can't set character set\n");

  if(mysql_query(&conn1, new_str) != 0)
     puterror("Error: can't execute SQL-query\n");
  free(new_str);
  new_str=NULL;
  res1 = mysql_store_result(&conn1);
  if(res1 == NULL) puterror("Error: can't get the result description\n");

  if(mysql_num_rows(res1) > 0)
  {
    while((row1 = mysql_fetch_row(res1)) != NULL)
    {
    	insQuery = "";
      fprintf(stdout, "%s,%s,%s,%s,%s\n", row1[0], row1[1], row1[2], row1[3], row1[4]);
      if(strncmp(row1[1],"71",2)==0){
      	if (strncmp(row1[3],"20",2)==0)
      	{
      		insQuery = "INSERT INTO lib_book_language (book_language_id, book_language_type_id, book_id) VALUES (1, 1, ";
      		char * new_ins_str;
          if((new_ins_str = malloc(strlen(insQuery)+strlen(book_id)+10)) != NULL){
            new_ins_str[0] = '\0';   // ensures the memory is an empty string
            strcat(new_ins_str,insQuery);
            strcat(new_ins_str,book_id);
            strcat(new_ins_str,")");
          } else {
            printf("malloc failed!\n");
            // exit?
          }
      		insertLang(new_ins_str);
      		free(new_ins_str);
      		new_ins_str=NULL;
      	}
      	if (strncmp(row1[3],"22",2)==0)
      	{
      		insQuery = "INSERT INTO lib_book_language (book_language_id, book_language_type_id, book_id) VALUES (7, 1, ";
      		char * new_ins_str;
          if((new_ins_str = malloc(strlen(insQuery)+strlen(book_id)+10)) != NULL){
            new_ins_str[0] = '\0';   // ensures the memory is an empty string
            strcat(new_ins_str,insQuery);
            strcat(new_ins_str,book_id);
            strcat(new_ins_str,")");
          } else {
            printf("malloc failed!\n");
            // exit?
          }
      		insertLang(new_ins_str);
      		free(new_ins_str);
      		new_ins_str=NULL;
      	}
      	if (strncmp(row1[3],"23",2)==0)
      	{
      		insQuery = "INSERT INTO lib_book_language (book_language_id, book_language_type_id, book_id) VALUES (2, 1, ";
      		char * new_ins_str;
          if((new_ins_str = malloc(strlen(insQuery)+strlen(book_id)+10)) != NULL){
            new_ins_str[0] = '\0';   // ensures the memory is an empty string
            strcat(new_ins_str,insQuery);
            strcat(new_ins_str,book_id);
            strcat(new_ins_str,")");
          } else {
            printf("malloc failed!\n");
            // exit?
          }
      		insertLang(new_ins_str);
      		free(new_ins_str);
      		new_ins_str=NULL;
      	}
      	if (strncmp(row1[3],"24",2)==0)
      	{
      		insQuery = "INSERT INTO lib_book_language (book_language_id, book_language_type_id, book_id) VALUES (1, 1, ";
      		char * new_ins_str;
          if((new_ins_str = malloc(strlen(insQuery)+strlen(book_id)+10)) != NULL){
            new_ins_str[0] = '\0';   // ensures the memory is an empty string
            strcat(new_ins_str,insQuery);
            strcat(new_ins_str,book_id);
            strcat(new_ins_str,")");
          } else {
            printf("malloc failed!\n");
            // exit?
          }
      		insertLang(new_ins_str);
      		free(new_ins_str);
      		new_ins_str=NULL;
      	}
      	if (strncmp(row1[3],"25",2)==0)
      	{
      		insQuery = "INSERT INTO lib_book_language (book_language_id, book_language_type_id, book_id) VALUES (5, 1, ";
      		char * new_ins_str;
          if((new_ins_str = malloc(strlen(insQuery)+strlen(book_id)+10)) != NULL){
            new_ins_str[0] = '\0';   // ensures the memory is an empty string
            strcat(new_ins_str,insQuery);
            strcat(new_ins_str,book_id);
            strcat(new_ins_str,")");
          } else {
            printf("malloc failed!\n");
            // exit?
          }
      		insertLang(new_ins_str);
      		free(new_ins_str);
      		new_ins_str=NULL;
      	}
      }

    }
  }
  free(insQuery);
  insQuery=NULL;
  mysql_free_result(res1);
  mysql_close(&conn1);
}

void insertLang(char * ins_str){
  MYSQL conn2;
  MYSQL_RES *res2;
  MYSQL_ROW row2;

  if(!mysql_init(&conn2))
     puterror("Error: can't create MySQL-descriptor\n");

  if(!mysql_real_connect(&conn2, "localhost", "root", "password", "Almata", 0, NULL,0))
     puterror("Error: can't connect to MySQL server\n");

  if(mysql_query(&conn2, "SET NAMES 'utf8'") != 0)
     puterror("Error: can't set character set\n");
  printf("%s\n", ins_str);
  if(mysql_query(&conn2, ins_str) != 0)
     puterror("Error: can't execute SQL-query\n");
  free(ins_str); 
  ins_str=NULL;
  mysql_free_result(res2);
  mysql_close(&conn2);
}

void puterror(char * str)
{
  printf("%s", str);
  exit(1);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-06-02
@dmtrrr

Use gdb to parse the core dump

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question