mysql - 堆栈粉碎错误

标签 mysql c stdin fgets stdio

这是我的代码:

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

char *server = "nope";
    char *user = "nope";
    char *password = "nope";
    char *database = "nope";
    unsigned int port = 0;

int cprint(char *text){//Imprime como encabezado el numero de productos sin stock sumado al argumento
    MYSQL *conn;
    MYSQL_RES *res;
    my_ulonglong num;

    conn = mysql_init(NULL);

    /* Connect to database */
    if (!mysql_real_connect(conn, server,
    user, password, database, port, NULL, 0)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }

    /* send SQL query */
    if (mysql_query(conn, "SELECT id FROM productos WHERE stock <= 0")) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }

    res = mysql_store_result(conn);
    num = mysql_num_rows(res);

    printf("\n===============================\n\n");
    if(num > 0){
        printf("Productos sin stock: %llu\n",num);
    }
    else{
        printf("No hay productos sin stock\n");
    }
    printf("\n===============================\n\n%s",text);   

    /*close connection */
    mysql_free_result(res);
    mysql_close(conn);

return 0;
}

char *remove_newline(char *s)
{
    int len = strlen(s);

    if (len > 0 && s[len-1] == '\n')  // if there's a newline
        s[len-1] = '\0';          // truncate the string

    return s;
}

int newproduct(){
    char *name = malloc(127*sizeof(char));//nombre del producto
    char *desc = malloc(127*sizeof(char));//descripcion del producto
    double price;//precio del producto
    cprint("Agregar nuevo producto\n\n");
    printf("Nombre del producto: ");
    fgets(name, 127, stdin);
    name = remove_newline(name);
    printf("Descripcion: ");
    fgets(desc, 127, stdin);
    name = remove_newline(desc);
    printf("Precio: ");
    scanf("%e", &price);

    MYSQL *conn;
    conn = mysql_init(NULL);
    printf("Mysql initiated\n");
    if (!mysql_real_connect(conn, server,
    user, password, database, port, NULL, 0)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }
    printf("connection established\n");
    char rname[256];//string donde guardar el nombre con caracteres de escape
    char rdesc[256];
    printf("Vars declared\n");
    mysql_real_escape_string(conn,rname,name,256);//se agregan los caracteres de escape
    printf("name escaped\n");
    mysql_real_escape_string(conn,rdesc,desc,256);
    printf("desc escaped\n");
    /*
    char *query;//donde guardar el query

    snprintf(query,1000,"INSERT INTO productos (nombre,descripcion,stock,precio) VALUES( %s,%s, 0, %e)",rname,rdesc,price);//query a enviar

    if (mysql_query(conn, query)) {//enviar el query
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }*/

    mysql_close(conn);
    printf("Mysql closed\n");
return 0;
}


int main(){
    printf("Sales Assistant Alpha v0.0\n");//Nombre y version del programa
    unsigned int choice;//numero de eleccion del menu
    char *err = NULL;//Error
    while(1){//loop infinito
        cprint("Menu Principal\n\n");//imprime el encabezado
        printf("1-Agregar nuevo producto\n");
        printf("2-Editar producto existente\n");
        printf("3-Productos sin stock\n");
        printf("4-Agregar pedido\n");
        printf("5-Salir de la aplicacion\n\n");

        if(err != NULL){//evalua si hubo un error y lo imprime
            printf("%s\n",err);
            err = NULL;
        }



        printf("Numero de eleccion: ");
        scanf("%i",&choice);//pide eleccion
        if (scanf("%*[^\n]") != EOF){//read and discard all non-newlines
        scanf("%*c"); //then read and discard the newline
        }
        switch(choice){//evalua la eleccion y ejecuta la funcion correspondiente
            case 1:
                newproduct();
                printf("returned\n");
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                exit(0);//termina el programa
                break;
            default:
                err = "Eleccion no valida";//error
                break;
        }
    }
return 0;
}

和控制台执行:

Sales Assistant Alpha v0.0

===============================

Productos sin stock: 3

===============================

Menu Principal

1-Agregar nuevo producto
2-Editar producto existente
3-Productos sin stock
4-Agregar pedido
5-Salir de la aplicacion

Numero de eleccion: 1

===============================

Productos sin stock: 3

===============================

Agregar nuevo producto

Nombre del producto: a
Descripcion: a
Precio: 1
Mysql initiated
connection established
Vars declared
name escaped
desc escaped
Mysql closed
*** stack smashing detected ***: ./sales_assistant terminated
Aborted (core dumped)


------------------
(program exited with code: 134)
Press return to continue

我有这个堆栈粉碎的东西,我不知道。除此之外,我想知道我可以安全地分配多少内存而不会出现总线错误。

重要提示:当我禁用 mysql_real_escape_string 函数时,代码正常工作,所以问题就在那里,但我不知道问题是什么。

谢谢,马特。

已编辑 添加了完整的代码并翻译成英文。也改变了问题,因为我得到了一个新的错误。添加了调试代码。

最佳答案

当您使用 scanf("%i", &choice); 请求一个数字时(应该检查其结果!),您将换行符留在后面。当您在 newproduct() 中使用 fgets() 读取一行时,它会读取留下的换行符。

最后,如果您正在进行基于行的输入,请使用 fgets()readline()(来自 POSIX 2008)来读取行和 sscanf() 来解析行。

关于mysql - 堆栈粉碎错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16408050/

相关文章:

linux - 如何测试STDIN读取错误

c - 如何读取 C 文件的内容并使用 fgets() 将其存储到数组中?

c - 为什么我使用与其他人相同的代码得到不同的结果?

php - php 和 mysql 中的组存档

php - 合并多个对象

c - Matlab 2013b 生成的代码在 mex 中生成 Undefined symbols for architecture x86_64 错误

c - 十六进制数组和十六进制文字的区别

mysql - 使用内部查询进行查询优化

mysql - 需要帮助构建 SQL SELECT 语句以获取 "top 10 x"

c - 使用 Str(n)cat 的 Pebble 堆损坏