sqlite3 2.10.0-x86_64-linux-gnu → 2.10.2-x86_64-linux-gnu

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -74,51 +74,6 @@ close_or_discard_db(sqlite3RubyPtr ctx)
74
74
  }
75
75
 
76
76
 
77
- void
78
- rb_sqlite3_pin_array_and_contents(VALUE ary)
79
- {
80
- long i;
81
-
82
- if (NIL_P(ary) || !ary) { return; }
83
-
84
- rb_gc_mark(ary);
85
- for (i = 0; i < RARRAY_LEN(ary); i++) {
86
- rb_gc_mark(RARRAY_AREF(ary, i));
87
- }
88
- }
89
-
90
- static int
91
- pin_hash_value(VALUE UNUSED(key), VALUE value, VALUE UNUSED(arg))
92
- {
93
- rb_gc_mark(value);
94
- return ST_CONTINUE;
95
- }
96
-
97
- static void
98
- pin_hash_and_contents(VALUE hash)
99
- {
100
- if (NIL_P(hash) || !hash) { return; }
101
-
102
- rb_gc_mark(hash);
103
- rb_hash_foreach(hash, pin_hash_value, 0);
104
- }
105
-
106
- /* Each wrapper also owns live aggregate instances, whose VALUEs sqlite keeps in
107
- * its own aggregate contexts. */
108
- static void
109
- pin_aggregators(VALUE aggregators)
110
- {
111
- long i;
112
-
113
- rb_sqlite3_pin_array_and_contents(aggregators);
114
-
115
- if (NIL_P(aggregators) || !aggregators) { return; }
116
-
117
- for (i = 0; i < RARRAY_LEN(aggregators); i++) {
118
- rb_sqlite3_aggregator_pin_instances(RARRAY_AREF(aggregators, i));
119
- }
120
- }
121
-
122
77
  static void
123
78
  database_mark(void *ctx)
124
79
  {
@@ -130,8 +85,8 @@ database_mark(void *ctx)
130
85
  rb_gc_mark(c->authorizer);
131
86
 
132
87
  rb_sqlite3_pin_array_and_contents(c->functions);
133
- pin_hash_and_contents(c->collations);
134
- pin_aggregators(c->aggregators);
88
+ rb_sqlite3_pin_array_and_contents(c->collations);
89
+ rb_sqlite3_aggregator_pin(c->aggregators);
135
90
  }
136
91
 
137
92
  static void
@@ -150,7 +105,7 @@ database_memsize(const void *ctx)
150
105
  }
151
106
 
152
107
  static const rb_data_type_t database_type = {
153
- .wrap_struct_name = "SQLite3::Backup",
108
+ .wrap_struct_name = "SQLite3::Database",
154
109
  .function = {
155
110
  .dmark = database_mark,
156
111
  .dfree = deallocate,
@@ -321,11 +276,21 @@ total_changes(VALUE self)
321
276
  return INT2NUM(sqlite3_total_changes(ctx->db));
322
277
  }
323
278
 
279
+ static VALUE
280
+ tracefunc_protected(VALUE args_value)
281
+ {
282
+ VALUE *args = (VALUE *)args_value;
283
+ return rb_funcall(args[0], rb_intern("call"), 1, rb_str_new2((const char *)args[1]));
284
+ }
285
+
324
286
  static void
325
287
  tracefunc(void *data, const char *sql)
326
288
  {
327
289
  sqlite3RubyPtr ctx = (sqlite3RubyPtr)data;
328
- rb_funcall(ctx->trace_handler, rb_intern("call"), 1, rb_str_new2(sql));
290
+ VALUE args[] = { ctx->trace_handler, (VALUE)sql };
291
+ int failed;
292
+
293
+ rb_sqlite3_protect_callback(tracefunc_protected, (VALUE)args, &failed);
329
294
  }
330
295
 
331
296
  /* call-seq:
@@ -356,15 +321,24 @@ trace(int argc, VALUE *argv, VALUE self)
356
321
  return self;
357
322
  }
358
323
 
324
+ static VALUE
325
+ rb_sqlite3_busy_handler_protected(VALUE args_value)
326
+ {
327
+ VALUE *args = (VALUE *)args_value;
328
+ return rb_funcall(args[0], rb_intern("call"), 1, INT2NUM((int)args[1]));
329
+ }
330
+
359
331
  static int
360
332
  rb_sqlite3_busy_handler(void *context, int count)
361
333
  {
362
334
  sqlite3RubyPtr ctx = (sqlite3RubyPtr)context;
335
+ VALUE args[] = { ctx->busy_handler, (VALUE)count };
336
+ VALUE result;
337
+ int failed;
363
338
 
364
- VALUE handle = ctx->busy_handler;
365
- VALUE result = rb_funcall(handle, rb_intern("call"), 1, INT2NUM(count));
339
+ result = rb_sqlite3_protect_callback(rb_sqlite3_busy_handler_protected, (VALUE)args, &failed);
366
340
 
367
- if (Qfalse == result) { return 0; }
341
+ if (failed || Qfalse == result) { return 0; }
368
342
 
369
343
  return 1;
370
344
  }
@@ -529,14 +503,14 @@ set_sqlite3_func_result(sqlite3_context *ctx, VALUE result)
529
503
  sqlite3_result_blob(
530
504
  ctx,
531
505
  (const void *)StringValuePtr(result),
532
- (int)RSTRING_LEN(result),
506
+ RSTRING_LENINT(result),
533
507
  SQLITE_TRANSIENT
534
508
  );
535
509
  } else {
536
510
  sqlite3_result_text(
537
511
  ctx,
538
512
  (const char *)StringValuePtr(result),
539
- (int)RSTRING_LEN(result),
513
+ RSTRING_LENINT(result),
540
514
  SQLITE_TRANSIENT
541
515
  );
542
516
  }
@@ -578,13 +552,13 @@ static void
578
552
  rb_sqlite3_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
579
553
  {
580
554
  rb_sqlite3_func_args_t args = { .ctx = ctx, .argc = argc, .argv = argv };
581
- int exc_status;
555
+ int failed;
582
556
 
583
- rb_protect(rb_sqlite3_func_protected, (VALUE)&args, &exc_status);
557
+ rb_sqlite3_protect_callback(rb_sqlite3_func_protected, (VALUE)&args, &failed);
584
558
 
585
- if (exc_status) {
559
+ if (failed) {
586
560
  /* the user should never see this message, because Statement#step will
587
- * re-raise the exception still in rb_errinfo */
561
+ * resume the Ruby exception instead */
588
562
  sqlite3_result_error(ctx, "Ruby Exception occurred", -1);
589
563
  }
590
564
  }
@@ -722,6 +696,33 @@ changes(VALUE self)
722
696
  return INT2NUM(sqlite3_changes(ctx->db));
723
697
  }
724
698
 
699
+ typedef struct {
700
+ VALUE authorizer;
701
+ int action;
702
+ const char *a;
703
+ const char *b;
704
+ const char *c;
705
+ const char *d;
706
+ } rb_sqlite3_auth_args_t;
707
+
708
+ static VALUE
709
+ rb_sqlite3_auth_protected(VALUE auth_args_value)
710
+ {
711
+ rb_sqlite3_auth_args_t *args = (rb_sqlite3_auth_args_t *)auth_args_value;
712
+ VALUE action = INT2NUM(args->action);
713
+ VALUE a = args->a ? rb_str_new2(args->a) : Qnil;
714
+ VALUE b = args->b ? rb_str_new2(args->b) : Qnil;
715
+ VALUE c = args->c ? rb_str_new2(args->c) : Qnil;
716
+ VALUE d = args->d ? rb_str_new2(args->d) : Qnil;
717
+ VALUE result = rb_funcall(args->authorizer, rb_intern("call"), 5, action, a, b, c, d);
718
+
719
+ if (T_FIXNUM == TYPE(result)) { return INT2FIX(NUM2INT(result)); }
720
+ if (Qtrue == result) { return INT2FIX(SQLITE_OK); }
721
+ if (Qfalse == result) { return INT2FIX(SQLITE_DENY); }
722
+
723
+ return INT2FIX(SQLITE_IGNORE);
724
+ }
725
+
725
726
  static int
726
727
  rb_sqlite3_auth(
727
728
  void *ctx,
@@ -732,18 +733,17 @@ rb_sqlite3_auth(
732
733
  const char *_d)
733
734
  {
734
735
  sqlite3RubyPtr db_ctx = (sqlite3RubyPtr)ctx;
735
- VALUE action = INT2NUM(_action);
736
- VALUE a = _a ? rb_str_new2(_a) : Qnil;
737
- VALUE b = _b ? rb_str_new2(_b) : Qnil;
738
- VALUE c = _c ? rb_str_new2(_c) : Qnil;
739
- VALUE d = _d ? rb_str_new2(_d) : Qnil;
740
- VALUE result = rb_funcall(db_ctx->authorizer, rb_intern("call"), 5, action, a, b, c, d);
741
-
742
- if (T_FIXNUM == TYPE(result)) { return (int)NUM2INT(result); }
743
- if (Qtrue == result) { return SQLITE_OK; }
744
- if (Qfalse == result) { return SQLITE_DENY; }
745
-
746
- return SQLITE_IGNORE;
736
+ rb_sqlite3_auth_args_t args = {
737
+ .authorizer = db_ctx->authorizer, .action = _action, .a = _a, .b = _b, .c = _c, .d = _d
738
+ };
739
+ VALUE result;
740
+ int failed;
741
+
742
+ result = rb_sqlite3_protect_callback(rb_sqlite3_auth_protected, (VALUE)&args, &failed);
743
+
744
+ if (failed) { return SQLITE_DENY; }
745
+
746
+ return FIX2INT(result);
747
747
  }
748
748
 
749
749
  /* call-seq: set_authorizer = auth
@@ -815,10 +815,19 @@ set_extended_result_codes(VALUE self, VALUE enable)
815
815
  return self;
816
816
  }
817
817
 
818
- int
819
- rb_comparator_func(void *ctx, int a_len, const void *a, int b_len, const void *b)
820
- {
818
+ typedef struct {
821
819
  VALUE comparator;
820
+ int a_len;
821
+ const void *a;
822
+ int b_len;
823
+ const void *b;
824
+ int comparison;
825
+ } rb_comparator_args_t;
826
+
827
+ static VALUE
828
+ rb_comparator_protected(VALUE comparator_args_value)
829
+ {
830
+ rb_comparator_args_t *args = (rb_comparator_args_t *)comparator_args_value;
822
831
  VALUE a_str;
823
832
  VALUE b_str;
824
833
  VALUE comparison;
@@ -826,9 +835,8 @@ rb_comparator_func(void *ctx, int a_len, const void *a, int b_len, const void *b
826
835
 
827
836
  internal_encoding = rb_default_internal_encoding();
828
837
 
829
- comparator = (VALUE)ctx;
830
- a_str = rb_str_new((const char *)a, a_len);
831
- b_str = rb_str_new((const char *)b, b_len);
838
+ a_str = rb_str_new((const char *)args->a, args->a_len);
839
+ b_str = rb_str_new((const char *)args->b, args->b_len);
832
840
 
833
841
  rb_enc_associate_index(a_str, rb_utf8_encindex());
834
842
  rb_enc_associate_index(b_str, rb_utf8_encindex());
@@ -838,9 +846,30 @@ rb_comparator_func(void *ctx, int a_len, const void *a, int b_len, const void *b
838
846
  b_str = rb_str_export_to_enc(b_str, internal_encoding);
839
847
  }
840
848
 
841
- comparison = rb_funcall(comparator, rb_intern("compare"), 2, a_str, b_str);
849
+ comparison = rb_funcall(args->comparator, rb_intern("compare"), 2, a_str, b_str);
850
+
851
+ args->comparison = NUM2INT(comparison);
842
852
 
843
- return NUM2INT(comparison);
853
+ return Qnil;
854
+ }
855
+
856
+ int
857
+ rb_comparator_func(void *ctx, int a_len, const void *a, int b_len, const void *b)
858
+ {
859
+ rb_comparator_args_t args = { .comparator = (VALUE)ctx, .a_len = a_len, .a = a, .b_len = b_len, .b = b };
860
+ int failed;
861
+
862
+ rb_sqlite3_protect_callback(rb_comparator_protected, (VALUE)&args, &failed);
863
+
864
+ /* sqlite has no way to report a comparator error */
865
+ return failed ? 0 : args.comparison;
866
+ }
867
+
868
+ static int
869
+ push_hash_value(VALUE UNUSED(key), VALUE value, VALUE ary)
870
+ {
871
+ rb_ary_push(ary, value);
872
+ return ST_CONTINUE;
844
873
  }
845
874
 
846
875
  /* call-seq: db.collation(name, comparator)
@@ -854,21 +883,42 @@ static VALUE
854
883
  collation(VALUE self, VALUE name, VALUE comparator)
855
884
  {
856
885
  sqlite3RubyPtr ctx;
857
- VALUE collations;
886
+ VALUE registry, key, comparators;
887
+ char *key_ptr;
888
+ long i;
858
889
  TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
859
890
  REQUIRE_OPEN_DB(ctx);
860
891
 
892
+ /* sqlite holds a raw pointer to the comparator, so keep it alive and
893
+ * unmoved. The registry is private because callers can mutate the public
894
+ * @collations hash, and the snapshot is an array so database_mark can pin
895
+ * it without iterating a hash. sqlite matches collation names without
896
+ * regard to ASCII case, so the registry does too. The key and registry
897
+ * are prepared before sqlite takes the pointer, and the snapshot is
898
+ * published before the public hash, whose write can raise. */
899
+ key = rb_str_new_cstr(StringValuePtr(name));
900
+ key_ptr = RSTRING_PTR(key);
901
+ for (i = 0; i < RSTRING_LEN(key); i++) {
902
+ if (key_ptr[i] >= 'A' && key_ptr[i] <= 'Z') { key_ptr[i] += 'a' - 'A'; }
903
+ }
904
+ if (!rb_ivar_defined(self, rb_intern("-collations"))) {
905
+ rb_iv_set(self, "-collations", rb_hash_new());
906
+ }
907
+ registry = rb_iv_get(self, "-collations");
908
+
861
909
  CHECK(ctx->db, sqlite3_create_collation(
862
910
  ctx->db,
863
- StringValuePtr(name),
911
+ RSTRING_PTR(name),
864
912
  SQLITE_UTF8,
865
913
  (void *)comparator,
866
914
  NIL_P(comparator) ? NULL : rb_comparator_func));
867
915
 
868
- /* sqlite holds a raw pointer to the comparator, so keep it alive and unmoved. */
869
- collations = rb_iv_get(self, "@collations");
870
- rb_hash_aset(collations, name, comparator);
871
- RB_OBJ_WRITE(self, &ctx->collations, collations);
916
+ rb_hash_aset(registry, key, comparator);
917
+ comparators = rb_ary_new();
918
+ rb_hash_foreach(registry, push_hash_value, comparators);
919
+ RB_OBJ_WRITE(self, &ctx->collations, comparators);
920
+
921
+ rb_hash_aset(rb_iv_get(self, "@collations"), name, comparator);
872
922
 
873
923
  return self;
874
924
  }
@@ -934,42 +984,68 @@ transaction_active_p(VALUE self)
934
984
  return sqlite3_get_autocommit(ctx->db) ? Qfalse : Qtrue;
935
985
  }
936
986
 
937
- static int
938
- hash_callback_function(VALUE callback_ary, int count, char **data, char **columns)
987
+ typedef struct {
988
+ VALUE callback_ary;
989
+ VALUE (*build_row)(VALUE);
990
+ } rb_sqlite3_exec_batch_t;
991
+
992
+ typedef struct {
993
+ VALUE callback_ary;
994
+ int count;
995
+ char **data;
996
+ char **columns;
997
+ } rb_sqlite3_exec_row_t;
998
+
999
+ static VALUE
1000
+ hash_callback_protected(VALUE row_value)
939
1001
  {
1002
+ rb_sqlite3_exec_row_t *row = (rb_sqlite3_exec_row_t *)row_value;
940
1003
  VALUE new_hash = rb_hash_new();
941
1004
  int i;
942
1005
 
943
- for (i = 0; i < count; i++) {
944
- if (data[i] == NULL) {
945
- rb_hash_aset(new_hash, rb_str_new_cstr(columns[i]), Qnil);
1006
+ for (i = 0; i < row->count; i++) {
1007
+ if (row->data[i] == NULL) {
1008
+ rb_hash_aset(new_hash, rb_str_new_cstr(row->columns[i]), Qnil);
946
1009
  } else {
947
- rb_hash_aset(new_hash, rb_str_new_cstr(columns[i]), rb_str_new_cstr(data[i]));
1010
+ rb_hash_aset(new_hash, rb_str_new_cstr(row->columns[i]), rb_str_new_cstr(row->data[i]));
948
1011
  }
949
1012
  }
950
1013
 
951
- rb_ary_push(callback_ary, new_hash);
1014
+ rb_ary_push(row->callback_ary, new_hash);
952
1015
 
953
- return 0;
1016
+ return Qnil;
954
1017
  }
955
1018
 
956
- static int
957
- regular_callback_function(VALUE callback_ary, int count, char **data, char **columns)
1019
+ static VALUE
1020
+ regular_callback_protected(VALUE row_value)
958
1021
  {
1022
+ rb_sqlite3_exec_row_t *row = (rb_sqlite3_exec_row_t *)row_value;
959
1023
  VALUE new_ary = rb_ary_new();
960
1024
  int i;
961
1025
 
962
- for (i = 0; i < count; i++) {
963
- if (data[i] == NULL) {
1026
+ for (i = 0; i < row->count; i++) {
1027
+ if (row->data[i] == NULL) {
964
1028
  rb_ary_push(new_ary, Qnil);
965
1029
  } else {
966
- rb_ary_push(new_ary, rb_str_new_cstr(data[i]));
1030
+ rb_ary_push(new_ary, rb_str_new_cstr(row->data[i]));
967
1031
  }
968
1032
  }
969
1033
 
970
- rb_ary_push(callback_ary, new_ary);
1034
+ rb_ary_push(row->callback_ary, new_ary);
971
1035
 
972
- return 0;
1036
+ return Qnil;
1037
+ }
1038
+
1039
+ static int
1040
+ exec_batch_callback_function(void *batch_ptr, int count, char **data, char **columns)
1041
+ {
1042
+ rb_sqlite3_exec_batch_t *batch = (rb_sqlite3_exec_batch_t *)batch_ptr;
1043
+ rb_sqlite3_exec_row_t row = { .callback_ary = batch->callback_ary, .count = count, .data = data, .columns = columns };
1044
+ int failed;
1045
+
1046
+ rb_sqlite3_protect_callback(batch->build_row, (VALUE)&row, &failed);
1047
+
1048
+ return failed;
973
1049
  }
974
1050
 
975
1051
 
@@ -987,21 +1063,27 @@ exec_batch(VALUE self, VALUE sql, VALUE results_as_hash)
987
1063
  sqlite3RubyPtr ctx;
988
1064
  int status;
989
1065
  VALUE callback_ary = rb_ary_new();
1066
+ rb_sqlite3_exec_batch_t batch = {
1067
+ .callback_ary = callback_ary,
1068
+ .build_row = results_as_hash == Qtrue ? hash_callback_protected : regular_callback_protected
1069
+ };
1070
+ rb_sqlite3_callback_frame_t frame;
1071
+ int callback_state;
1072
+ const char *sql_cstr;
990
1073
  char *errMsg;
991
1074
 
992
1075
  TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
993
1076
  REQUIRE_OPEN_DB(ctx);
994
1077
 
1078
+ sql_cstr = StringValuePtr(sql);
995
1079
  timespecclear(&ctx->stmt_deadline);
996
1080
 
997
- if (results_as_hash == Qtrue) {
998
- status = sqlite3_exec(ctx->db, StringValuePtr(sql), (sqlite3_callback)hash_callback_function,
999
- (void *)callback_ary,
1000
- &errMsg);
1001
- } else {
1002
- status = sqlite3_exec(ctx->db, StringValuePtr(sql), (sqlite3_callback)regular_callback_function,
1003
- (void *)callback_ary,
1004
- &errMsg);
1081
+ rb_sqlite3_push_callback_frame(&frame);
1082
+ status = sqlite3_exec(ctx->db, sql_cstr, exec_batch_callback_function, (void *)&batch, &errMsg);
1083
+
1084
+ if ((callback_state = rb_sqlite3_pop_callback_frame(&frame))) {
1085
+ sqlite3_free(errMsg);
1086
+ rb_jump_tag(callback_state);
1005
1087
  }
1006
1088
 
1007
1089
  CHECK_MSG(ctx->db, status, errMsg);
@@ -10,6 +10,12 @@
10
10
  struct _sqlite3Ruby {
11
11
  sqlite3 *db;
12
12
  VALUE busy_handler;
13
+ /* sqlite holds raw pointers to everything in these arrays. functions and
14
+ * aggregators are append-only, so redefining a name keeps the old object
15
+ * for the life of the connection: sqlite keys them by name and arity, and
16
+ * releasing on name alone was GHSA-28hh-pr2h-2w89. collations is rebuilt
17
+ * from a private hash keyed by name without regard to ASCII case, as
18
+ * sqlite keys them, so it releases a replaced comparator. */
13
19
  VALUE functions;
14
20
  VALUE collations;
15
21
  VALUE aggregators;
@@ -24,9 +30,6 @@ struct _sqlite3Ruby {
24
30
  typedef struct _sqlite3Ruby sqlite3Ruby;
25
31
  typedef sqlite3Ruby *sqlite3RubyPtr;
26
32
 
27
- /* Pinning a collection doesn't pin what's in it, hence both. */
28
- void rb_sqlite3_pin_array_and_contents(VALUE ary);
29
-
30
33
  void init_sqlite3_database();
31
34
  void set_sqlite3_func_result(sqlite3_context *ctx, VALUE result);
32
35
 
@@ -120,3 +120,98 @@ rb_sqlite3_raise_with_sql(sqlite3 *db, int status, const char *sql)
120
120
 
121
121
  rb_exc_raise(exception);
122
122
  }
123
+
124
+ /* Each call into sqlite that can run callbacks pushes a frame on the current
125
+ * fiber's chain. A callback that fails records its rb_protect state in the top
126
+ * frame, and the call resumes Ruby's pending jump once sqlite returns. Ruby
127
+ * code that runs in between, such as a finalizer during an allocation, pushes
128
+ * its own frame for any nested call, so it never takes the outer call's jump.
129
+ * The chain is fiber-local because Ruby code inside a callback can switch
130
+ * threads or fibers, and a Ruby thread can move between native threads inside
131
+ * rb_protect. */
132
+ typedef struct {
133
+ rb_sqlite3_callback_frame_t *top;
134
+ } callback_frames_t;
135
+
136
+ static const rb_data_type_t callback_frames_type = {
137
+ .wrap_struct_name = "SQLite3::CallbackFrames",
138
+ .function = {
139
+ .dfree = RUBY_TYPED_DEFAULT_FREE,
140
+ },
141
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
142
+ };
143
+
144
+ static ID id_callback_frames;
145
+
146
+ /* Ruby code can overwrite or delete the fiber-local, so check its type. */
147
+ static VALUE
148
+ current_callback_frames_holder(void)
149
+ {
150
+ VALUE holder = rb_thread_local_aref(rb_thread_current(), id_callback_frames);
151
+ return rb_typeddata_is_kind_of(holder, &callback_frames_type) ? holder : Qnil;
152
+ }
153
+
154
+ static callback_frames_t *
155
+ current_callback_frames(void)
156
+ {
157
+ VALUE holder = current_callback_frames_holder();
158
+ return NIL_P(holder) ? NULL : (callback_frames_t *)RTYPEDDATA_DATA(holder);
159
+ }
160
+
161
+ void
162
+ rb_sqlite3_push_callback_frame(rb_sqlite3_callback_frame_t *frame)
163
+ {
164
+ VALUE holder = current_callback_frames_holder();
165
+ callback_frames_t *frames;
166
+
167
+ if (NIL_P(holder)) {
168
+ holder = TypedData_Make_Struct(rb_cObject, callback_frames_t, &callback_frames_type, frames);
169
+ rb_thread_local_aset(rb_thread_current(), id_callback_frames, holder);
170
+ }
171
+ frames = (callback_frames_t *)RTYPEDDATA_DATA(holder);
172
+
173
+ frame->state = 0;
174
+ frame->prev = frames->top;
175
+ frame->holder = holder;
176
+ frames->top = frame;
177
+ }
178
+
179
+ int
180
+ rb_sqlite3_pop_callback_frame(rb_sqlite3_callback_frame_t *frame)
181
+ {
182
+ ((callback_frames_t *)RTYPEDDATA_DATA(frame->holder))->top = frame->prev;
183
+ return frame->state;
184
+ }
185
+
186
+ VALUE
187
+ rb_sqlite3_protect_callback(VALUE (*body)(VALUE), VALUE data, int *failed)
188
+ {
189
+ callback_frames_t *frames = current_callback_frames();
190
+ rb_sqlite3_callback_frame_t *frame = frames ? frames->top : NULL;
191
+ VALUE result;
192
+ int state;
193
+
194
+ if (frame && frame->state) {
195
+ *failed = 1;
196
+ return Qnil;
197
+ }
198
+
199
+ result = rb_protect(body, data, &state);
200
+ *failed = state != 0;
201
+
202
+ if (frame) {
203
+ frame->state = state;
204
+ } else if (state) {
205
+ /* sqlite ran the callback from a call that cannot resume the jump, such
206
+ * as Statement#reset!, so the error is dropped */
207
+ rb_set_errinfo(Qnil);
208
+ }
209
+
210
+ return result;
211
+ }
212
+
213
+ void
214
+ init_sqlite3_exception(void)
215
+ {
216
+ id_callback_frames = rb_intern("__sqlite3_callback_frames__");
217
+ }
@@ -9,4 +9,25 @@ void rb_sqlite3_raise(sqlite3 *db, int status);
9
9
  void rb_sqlite3_raise_msg(sqlite3 *db, int status, const char *msg);
10
10
  void rb_sqlite3_raise_with_sql(sqlite3 *db, int status, const char *sql);
11
11
 
12
+ typedef struct rb_sqlite3_callback_frame {
13
+ int state;
14
+ struct rb_sqlite3_callback_frame *prev;
15
+ /* the frame lives on the C stack, so GC marks and pins this */
16
+ VALUE holder;
17
+ } rb_sqlite3_callback_frame_t;
18
+
19
+ /* A call into sqlite that can run callbacks is wrapped in a frame. sqlite
20
+ * cannot carry a raise or a throw, so a failed callback records its rb_protect
21
+ * state in the frame, and later callbacks skip their Ruby code. Pop the frame
22
+ * when sqlite returns, then resume the jump with rb_jump_tag if the returned
23
+ * state is nonzero. Run nothing but the sqlite call in between, because a
24
+ * raise there would skip the pop. */
25
+ void rb_sqlite3_push_callback_frame(rb_sqlite3_callback_frame_t *frame);
26
+ int rb_sqlite3_pop_callback_frame(rb_sqlite3_callback_frame_t *frame);
27
+
28
+ /* Runs a callback's Ruby code for sqlite. Sets *failed if it did not complete. */
29
+ VALUE rb_sqlite3_protect_callback(VALUE (*body)(VALUE), VALUE data, int *failed);
30
+
31
+ void init_sqlite3_exception(void);
32
+
12
33
  #endif
@@ -60,6 +60,19 @@ bignum_to_int64(VALUE value, sqlite3_int64 *result)
60
60
  #endif
61
61
  }
62
62
 
63
+ void
64
+ rb_sqlite3_pin_array_and_contents(VALUE ary)
65
+ {
66
+ long i;
67
+
68
+ if (NIL_P(ary) || !ary) { return; }
69
+
70
+ rb_gc_mark(ary);
71
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
72
+ rb_gc_mark(RARRAY_AREF(ary, i));
73
+ }
74
+ }
75
+
63
76
  static VALUE
64
77
  libversion(VALUE UNUSED(klass))
65
78
  {
@@ -191,6 +204,7 @@ Init_sqlite3_native(void)
191
204
  sqlite3_initialize();
192
205
  #endif
193
206
 
207
+ init_sqlite3_exception();
194
208
  init_sqlite3_constants();
195
209
  init_sqlite3_database();
196
210
  init_sqlite3_statement();
@@ -45,4 +45,7 @@ extern VALUE cSqlite3Blob;
45
45
 
46
46
  int bignum_to_int64(VALUE big, sqlite3_int64 *result);
47
47
 
48
+ /* Pinning a collection doesn't pin what's in it, hence both. */
49
+ void rb_sqlite3_pin_array_and_contents(VALUE ary);
50
+
48
51
  #endif