Thanks Olivier Mascia for the tips and suggestion I will definitely try out.

sorry for the code with the double pointer which has raised lot of
confusion I ensured this fault raised is not beacuse of any of my pointer
usage .

I am having a workaround with key/value memory allocation in heap and
double pointer seen was its residue, I am wondering why this fault occurs
for stack allocation.

int mydef_set(sqlite3 *db,char *key, char **value)
{
    char *zErrMsg = 0;
    int rc;
    char query[200]
    sprintf(query,"INSERT OR REPLACE INTO cosmos_db (key,value) values
('%s', '%s');",key,*value);
    /* Execute SQL statement */
    lastError = sqlite3_exec(db, query, 0, 0, &zErrMsg);
    if( lastError != SQLITE_OK ) {
                  fprintf(stderr, "SQL error: %s\n", zErrMsg);
                  sqlite3_free(zErrMsg);
           } else {
                  fprintf(stdout, "Update done successfully\n");
           }
    return lastError;
}


int main()
{


    sqlite3 *db;
    db=mod_init();
    char *val=malloc(1000);
        //strcpy(val,
        char dest[]="axzchsdjzcjsdjdcfsjhgfcshgsdfgsfg h
dbhjbbssdfsdsgffjhdsgfjg";
        strcpy(val,dest);

        mydef_set(db,"sssi",&val);

}


Ratheendran





On Sun, Oct 21, 2018 at 10:46 PM Olivier Mascia <o...@integral.be> wrote:

> Hi,
>
> mydef_set probably overflows your 'query' variable of which you don't show
> declaration but I guess it is 200 bytes seeing your memset(query,0,200);
> strcpy(query, ...
>
> This above and why this char** buffer in mydef_set prototype?
> Think about what your intent was. Compare to what you did (right) for key
> parameter.
>
> Try to stop writing software that sprintf things to fixed sized buffers.
> Especially when the purpose is to dynamically build SQL statements. Learn
> about using parameters in your SQL statement, prepare once and execute
> many, supplying values for the parameters at each run (step).
>
> Hope it will help.
> --
> Best Regards, Meilleures salutations, Met vriendelijke groeten,
> Olivier Mascia
>
> > Le 21 oct. 2018 à 18:54, Ratheendran R <ratheendra...@gmail.com> a
> écrit :
> >
> > Hi,
> >
> > I am a embedded engineer and new to sqlite,we want to use sqlite for our
> > local storage instead of file i/o.
> >
> > I have created a table with key and value records of char type,now if I
> try
> > store a value with string length more than 50 char I get segmentation
> > fault,please see the code below and let me know if I can improve it.
> >
> > sqlite3 *mod_init() {
> >
> >    /* Open database */
> >    //rc = sqlite3_open("test.db", &dbObj->db);
> >    lastError = sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE |
> > SQLITE_OPEN_CREATE | SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_NOMUTEX , NULL);
> >    if( lastError ) {
> >        fprintf(stderr, "Can't open database: %s\n",
> > sqlite3_errmsg(dbObj->db));
> >    free(query);
> >    return(0);
> >    } else {
> >        fprintf(stdout, "Opened database successfully\n");
> >    }
> >    memset(query,0,200);
> >    strcpy(query,"CREATE TABLE IF NOT EXISTS cosmos_db("  \
> >    "key   TEXT PRIMARY KEY       NOT NULL," \
> >    "value        VARCHAR(100));");
> >
> >    /* Execute SQL statement */
> >    lastError = sqlite3_exec(db, query, 0, 0, &zErrMsg);
> >
> >
> >    if( lastError != SQLITE_OK ){
> >        fprintf(stderr, "SQL error: %s\n", zErrMsg);
> >        sqlite3_free(zErrMsg);
> >    } else {
> >        fprintf(stdout, "Table created successfully\n");
> >    }
> > return db;
> > }
> >
> >
> >
> > int mydef_set(cf_db_t *dbObj,char *key, char **value)
> > {
> >    char *zErrMsg = 0;
> >    int rc;
> >    sprintf(query,"INSERT OR REPLACE INTO cosmos_db (key,value) values
> > ('%s', '%s');",key,*value);
> >    /* Execute SQL statement */
> >    lastError = sqlite3_exec(db, query, 0, 0, &zErrMsg);
> >    if( lastError != SQLITE_OK ) {
> >                  fprintf(stderr, "SQL error: %s\n", zErrMsg);
> >                  sqlite3_free(zErrMsg);
> >           } else {
> >                  fprintf(stdout, "Update done successfully\n");
> >           }
> >    return lastError;
> > }
> >
> >
> > int main()
> > {
> >    sqlite3 *db;
> >    db=mod_init();
> >        mydef_set(db,"sssi","Hitjkahzdsdhdjksdhjsdhsjfhjsdhfjhsjd bcn
> > bsdbgfhjsdgcsdfcbscbshdfgchdsfbbsdfcsfg");
> > }
> >
> > Thanks,
> > Ratheendran
> > _______________________________________________
> > sqlite-users mailing list
> > sqlite-users@mailinglists.sqlite.org
> > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
>
>
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to