Skip to content

Commit 23e2b76

Browse files
committed
Merge pull request #741 from kamipo/fix_segv_when_num_of_rows_is_zero
Fix segv when num of rows is zero
2 parents e8001bb + 6637c9f commit 23e2b76

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

ext/mysql2/result.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,15 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
346346
conn_enc = rb_to_encoding(wrapper->encoding);
347347
#endif
348348

349+
if (wrapper->fields == Qnil) {
350+
wrapper->numberOfFields = mysql_num_fields(wrapper->result);
351+
wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
352+
}
349353
if (args->asArray) {
350354
rowVal = rb_ary_new2(wrapper->numberOfFields);
351355
} else {
352356
rowVal = rb_hash_new();
353357
}
354-
if (wrapper->fields == Qnil) {
355-
wrapper->numberOfFields = mysql_num_fields(wrapper->result);
356-
wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
357-
}
358358

359359
if (wrapper->result_buffers == NULL) {
360360
rb_mysql_result_alloc_result_buffers(self, fields);
@@ -541,16 +541,16 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, MYSQL_FIELD * fields, const r
541541
return Qnil;
542542
}
543543

544+
if (wrapper->fields == Qnil) {
545+
wrapper->numberOfFields = mysql_num_fields(wrapper->result);
546+
wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
547+
}
544548
if (args->asArray) {
545549
rowVal = rb_ary_new2(wrapper->numberOfFields);
546550
} else {
547551
rowVal = rb_hash_new();
548552
}
549553
fieldLengths = mysql_fetch_lengths(wrapper->result);
550-
if (wrapper->fields == Qnil) {
551-
wrapper->numberOfFields = mysql_num_fields(wrapper->result);
552-
wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
553-
}
554554

555555
for (i = 0; i < wrapper->numberOfFields; i++) {
556556
VALUE field = rb_mysql_result_fetch_field(self, i, args->symbolizeKeys);
@@ -913,13 +913,8 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
913913
app_timezone = Qnil;
914914
}
915915

916-
if (wrapper->lastRowProcessed == 0 && !wrapper->is_streaming) {
916+
if (wrapper->rows == Qnil && !wrapper->is_streaming) {
917917
wrapper->numberOfRows = wrapper->stmt_wrapper ? mysql_stmt_num_rows(wrapper->stmt_wrapper->stmt) : mysql_num_rows(wrapper->result);
918-
if (wrapper->numberOfRows == 0) {
919-
rb_mysql_result_free_result(wrapper);
920-
wrapper->rows = rb_ary_new();
921-
return wrapper->rows;
922-
}
923918
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
924919
}
925920

0 commit comments

Comments
 (0)