sqlite3_aggregate_context() in xFinal callback does not zeroes out memory

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

sqlite3_aggregate_context() in xFinal callback does not zeroes out memory

Lifepillar
When I call a custom aggregate function on an empty table T, e.g.,
`select myaggr(A) from T`, the sqlite3_aggregate_context() call in the
xFinal callback does not return zeroed out memory, but a block of
seemingly uninitialized memory. Is that expected? If so, how do I know
that the xStep callback has not been called?

The code looks like this:

typedef struct Aggr Aggr;
struct Aggr {
   Value value;
   uint32_t count;
};

static void
myaggrStep(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
   Aggr* sum = (Aggr*)sqlite3_aggregate_context(ctx, sizeof(Aggr*));

   if (sum == 0) return;

   if (sqlite3_value_type(argv[0]) == SQLITE_NULL)
     return;

   if (sum->count == 0) { // First invocation
     // Initialize aggr->value
     sum->count++;
   }
   else {
     // Update aggr->value
     sum->count++;
   }
}

static void myaggrFinal(sqlite3_context* ctx) {
   Aggr* aggr = (Aggr*)sqlite3_aggregate_context(ctx, sizeof(Aggr*));
   if (aggr == 0) return;
   if (aggr->count == 0) { // (*)
     // xStep was not called, set default value
   }
   // Set result using aggr->value
}

(*) The problem is that in the situation described above, aggr->count
here is not zero.

Using SQLite 3.21.0.

Thanks,
Life.

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: sqlite3_aggregate_context() in xFinal callback does not zeroes out memory

Lifepillar
On 17/12/2017 11:40, Lifepillar wrote:

> When I call a custom aggregate function on an empty table T, e.g.,
> `select myaggr(A) from T`, the sqlite3_aggregate_context() call in the
> xFinal callback does not return zeroed out memory, but a block of
> seemingly uninitialized memory. Is that expected? If so, how do I know
> that the xStep callback has not been called?
>
> The code looks like this:
>
> typedef struct Aggr Aggr;
> struct Aggr {
>    Value value;
>    uint32_t count;
> };
>
> static void
> myaggrStep(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
>    Aggr* sum = (Aggr*)sqlite3_aggregate_context(ctx, sizeof(Aggr*));

Oh dear. Never mind, I have just noticed the spurious pointer.

Sorry for the noise,
Life.

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users