简体   繁体   中英

Segmentation fault problem, gdb message

I have been trying to understand why I get "SEGMENTATION FAULT" while running a program written in C languagge.

I tried gdb.

This is the message I got:

Program received signal SIGSEGV, Segmentation fault.
0x0016e5e0 in mysql_slave_send_query () from /usr/lib/libmysqlclient.so.16
(gdb) step
Single stepping until exit from function mysql_slave_send_query, 
which has no line number information.

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.

The fact is that I get no compiler error/warning messages but the program doesn't work properly.

Can anyone help me?

My query is:

char query[512];
    sprintf(query, "SELECT t1.Art_Acquisto as 'Cod',t2.Des_Articolo as 'Descrizione',t4.Cod_Categoria as 'Cat.',t1.Data_Acquisto as"                   "'data',t1.Netto_Acquisto as'Importo',t3.Des_Fornitore as 'Fornitore' from "
               "Aquisti as t1, Articoli as t2, Fornitori as t3, Categorie as t4  where t1.Art_Acquisto = t2.Cod_Articolo and "  
                       "t1.fornitoreM = t3.codiceF and t4.codiceC = t2.categoriaA and Art_Acquisto ='%s'order by Data_Acquisto;",Cod_Articolo);

检查您对mysql_slave_send_query的参数,尤其是其他参数的长度和正确的初始化/分配。

"Segmentation fault" means that your program is accessing an invalid memory location during the execution of the function named mysql_slave_send_query. Therefore this means there is a bug in your C program.

How big is the contents of Cod_Articolo ? Your query is nearly 400 characters, and the buffer you're storing it in is only 512 characters - if Cod_Articolo is over 110 characters, your code will break.

If this is the problem, you can prevent it by checking the size of Cod_Articolo before writing it into the query string. Even better, you could calculate the size the query string needs to be, and allocate exactly the right amount.

It's also good practice to use snprintf instead of sprintf , as then you can ensure you don't copy too many characters into the destination string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM