Re: [sqlite] SQLITE_STATIC and temporary data

2012-08-01 Thread Jay A. Kreibich
On Wed, Aug 01, 2012 at 04:48:48PM +, Rob Richardson scratched on the wall:
> Is "acceptable" good enough?  I admit I haven't played with this function
> (actually, I never heard of it until today), but from what I read in the
> documentation, the case described looked dangerous to me.

  It's somewhat undefined, and I suspect that's intentional.  There are
  a lot of specifics that are undocumented because they change from
  version to version of SQLite.

> SQLITE_STATIC
> seemed to me to imply that the contents of the memory used by the sqlite
> statement would never change over the life of the statement.

  It's more the lifetime of the binding.  Which reminds me (and I see
  others on the list already pointed this out), freeing the memory
  after a call to _reset() would be a Bad Idea since the binding is
  still in effect.  It would have to be _finalize() or _clear_bindings().

  But yes... the key is that the memory remains valid for the lifetime
  of the binding, not the fact that is or isn't statically allocated.

   -j

> -Original Message-
> From: sqlite-users-boun...@sqlite.org 
> [mailto:sqlite-users-boun...@sqlite.org] On Behalf Of Jay A. Kreibich
> Sent: Wednesday, August 01, 2012 12:16 PM
> To: General Discussion of SQLite Database
> Cc: c...@iki.fi
> Subject: Re: [sqlite] SQLITE_STATIC and temporary data
> 
> On Wed, Aug 01, 2012 at 08:49:19PM +1000, Yose Widjaja scratched on the wall:
> > Dear Friends,
> > 
> > So SQLITE_STATIC is meant to be used for data that is static. However, 
> > would it still be safe when it is used with data that expires after 
> > the
> > sqlite3_step() function?
> > 
> > For example:
> > 
> > string hello = "hello world";
> > 
> > sqlite3_bind(statement, 1, hello.c_str(), hello.size(), 
> > SQLITE_STATIC);
> > 
> > sqlite3_step(statement);
> > 
> > hello = "moo";
> > 
> > Would there be anything that can potentially go wrong? I mean, since 
> > SQLITE_STATIC is meant to imply static stuff, would sqlite cache the 
> > value in such a way that subsequence SELECT_ statements actually use 
> > this static value that was passed in through the bind function?
> 
>   It would be better to keep the value valid until _reset() or
>   _finalize() is called, but, yes... this use of SQLITE_STATIC is
>   acceptable (and somewhat common).
> 
>-j

-- 
Jay A. Kreibich < J A Y  @  K R E I B I.C H >

"Intelligence is like underwear: it is important that you have it,
 but showing it to the wrong people has the tendency to make them
 feel uncomfortable." -- Angela Johnson
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] SQLITE_STATIC and temporary data

2012-08-01 Thread Yuriy Kaminskiy
Jay A. Kreibich wrote:
> On Wed, Aug 01, 2012 at 08:49:19PM +1000, Yose Widjaja scratched on the wall:
>> Dear Friends,
>>
>> So SQLITE_STATIC is meant to be used for data that is static. However,
>> would it still be safe when it is used with data that expires after the
>> sqlite3_step() function?
>>
>> For example:
>>
>> string hello = "hello world";
>>
>> sqlite3_bind(statement, 1, hello.c_str(), hello.size(), SQLITE_STATIC);
>>
>> sqlite3_step(statement);

>> hello = "moo";
>>
>> Would there be anything that can potentially go wrong? I mean, since
>> SQLITE_STATIC is meant to imply static stuff, would sqlite cache the value
>> in such a way that subsequence SELECT_ statements actually use this static
>> value that was passed in through the bind function?
> 
>   It would be better to keep the value valid until _reset() or

No, _reset is NOT enough, as it *won't* clear bindings. You *must* keep data
till either sqlite3_clear_bindings or sqlite3_finalize is (successfully) called.

>   _finalize() is called, but, yes... this use of SQLITE_STATIC is
>   acceptable (and somewhat common).

Above code is certainly incorrect. Maybe you won't be slapped with SIGSEGV
immediately (depending on surrounding code and sqlite3 implementation details),
but still this is incorrect and unsafe. You should insert
   sqlite3_clear_bindings(statement);
or
   sqlite3_bind_null(statement, 1);
before hello = "moo";

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] SQLITE_STATIC and temporary data

2012-08-01 Thread Rob Richardson
Is "acceptable" good enough?  I admit I haven't played with this function 
(actually, I never heard of it until today), but from what I read in the 
documentation, the case described looked dangerous to me.  SQLITE_STATIC seemed 
to me to imply that the contents of the memory used by the sqlite statement 
would never change over the life of the statement.

But please keep in mind that in this case (as in many other cases), my opinion 
likely to be worth exactly what you have paid for it.

RobR

-Original Message-
From: sqlite-users-boun...@sqlite.org [mailto:sqlite-users-boun...@sqlite.org] 
On Behalf Of Jay A. Kreibich
Sent: Wednesday, August 01, 2012 12:16 PM
To: General Discussion of SQLite Database
Cc: c...@iki.fi
Subject: Re: [sqlite] SQLITE_STATIC and temporary data

On Wed, Aug 01, 2012 at 08:49:19PM +1000, Yose Widjaja scratched on the wall:
> Dear Friends,
> 
> So SQLITE_STATIC is meant to be used for data that is static. However, 
> would it still be safe when it is used with data that expires after 
> the
> sqlite3_step() function?
> 
> For example:
> 
> string hello = "hello world";
> 
> sqlite3_bind(statement, 1, hello.c_str(), hello.size(), 
> SQLITE_STATIC);
> 
> sqlite3_step(statement);
> 
> hello = "moo";
> 
> Would there be anything that can potentially go wrong? I mean, since 
> SQLITE_STATIC is meant to imply static stuff, would sqlite cache the 
> value in such a way that subsequence SELECT_ statements actually use 
> this static value that was passed in through the bind function?

  It would be better to keep the value valid until _reset() or
  _finalize() is called, but, yes... this use of SQLITE_STATIC is
  acceptable (and somewhat common).

   -j


--
Jay A. Kreibich < J A Y  @  K R E I B I.C H >

"Intelligence is like underwear: it is important that you have it,  but showing 
it to the wrong people has the tendency to make them  feel uncomfortable." -- 
Angela Johnson ___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] SQLITE_STATIC and temporary data

2012-08-01 Thread Jay A. Kreibich
On Wed, Aug 01, 2012 at 08:49:19PM +1000, Yose Widjaja scratched on the wall:
> Dear Friends,
> 
> So SQLITE_STATIC is meant to be used for data that is static. However,
> would it still be safe when it is used with data that expires after the
> sqlite3_step() function?
> 
> For example:
> 
> string hello = "hello world";
> 
> sqlite3_bind(statement, 1, hello.c_str(), hello.size(), SQLITE_STATIC);
> 
> sqlite3_step(statement);
> 
> hello = "moo";
> 
> Would there be anything that can potentially go wrong? I mean, since
> SQLITE_STATIC is meant to imply static stuff, would sqlite cache the value
> in such a way that subsequence SELECT_ statements actually use this static
> value that was passed in through the bind function?

  It would be better to keep the value valid until _reset() or
  _finalize() is called, but, yes... this use of SQLITE_STATIC is
  acceptable (and somewhat common).

   -j


-- 
Jay A. Kreibich < J A Y  @  K R E I B I.C H >

"Intelligence is like underwear: it is important that you have it,
 but showing it to the wrong people has the tendency to make them
 feel uncomfortable." -- Angela Johnson
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users