I’m trying to make a C program work and I’m getting mad. This is my code simplified to find the error:
#include <stdio.h>
#include <unistd.h>
#include <sqlite3.h>
int main(){
sqlite3 *conn;
sqlite3_stmt *res;
const char *tail, *sqlresult;
sqlite3_open("cubecat", &conn);
char buffer,query;
int id;
id= 1;
buffer = 'a';
if(buffer == 'a') snprintf(&query,100,"SELECT start FROM payloads WHERE id=%d", id);
printf("%s",&query);
int error = sqlite3_prepare_v2(conn, &query, 100, &res, &tail);
printf("%d",error);
}
The error is exactly on “sqlite_prepare_v2” function, because if I comment that line, there’s no Segmentation Fault.
Thank you in advance!
This is what’s wrong.
queryonly reserves memory for one character. There’s a reason the 2nd argument ofsnprintf()specifies the size. This code should be modified like this: