A couple of comments.

On Feb 23, 2007, at 3:03 PM, Cesar Rodas wrote:
   while ( (n=fread(buff,Buffsize,1,file)) > 0)
   {
       if (i>0)
           *value = realloc(*value, (i+1) * Buffsize);
       memcpy(*value + (i * Buffsize), buff,  Buffsize);
       *len += n;
       i++;
   }

You are growing the array in size at each append. This is usually a bad idea; it leads to n^2 complexity. The standard approach is to double the buffer each time it fills up. This is 2*n complexity. However, it's not your bug.

query = sqlite3_mprintf("INSERT INTO blob VALUES('%Q') ", value);

This segfaults because there is no null terminator on the value string. The way SQLite3 reads 'value' is by scanning it until it finds a '\0'. If there is no such character, it scans past the end of the array, leading to the crash you are seeing.

A simple solution is to append this character to the end of the string. However, it the file itself contains a '\0' character, this will lead to the file being truncated in the database. A better solution would be:

file_content = ... your loading code, allocated by malloc ...
file_length = ... length of the file ...;
query = "INSERT INTO blob VALUES(?);";
sqlite3_prepare_v2(db, query, -1, &qhandle, &errmsg);
sqlite3_bind_block(qhandle, 1, file_content, file_length, free);


-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to