Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit f40e17a

Browse files
committed
Merge pull request brianmario#742 from ajaska/0.3.x
Fix 0.3.x segfault for :cache_rows => false
2 parents aa74392 + 4885f9a commit f40e17a

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

ext/mysql2/result.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,10 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
531531
return wrapper->rows;
532532
}
533533
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
534+
} else if (!cacheRows && wrapper->lastRowProcessed == wrapper->numberOfRows) {
535+
mysql_data_seek(wrapper->result, 0);
536+
wrapper->lastRowProcessed = 0;
537+
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
534538
}
535539

536540
if (cacheRows && wrapper->lastRowProcessed == wrapper->numberOfRows) {
@@ -558,15 +562,17 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
558562

559563
if (row == Qnil) {
560564
/* we don't need the mysql C dataset around anymore, peace it */
561-
rb_mysql_result_free_result(wrapper);
565+
if (cacheRows) {
566+
rb_mysql_result_free_result(wrapper);
567+
}
562568
return Qnil;
563569
}
564570

565571
if (block != Qnil) {
566572
rb_yield(row);
567573
}
568574
}
569-
if (wrapper->lastRowProcessed == wrapper->numberOfRows) {
575+
if (wrapper->lastRowProcessed == wrapper->numberOfRows && cacheRows) {
570576
/* we don't need the mysql C dataset around anymore, peace it */
571577
rb_mysql_result_free_result(wrapper);
572578
}

spec/mysql2/result_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@
7878
result.first.object_id.should_not eql(result.first.object_id)
7979
end
8080

81+
it "should be able to iterate a second time even if cache_rows is disabled" do
82+
result = @client.query "SELECT 1 UNION SELECT 2", :cache_rows => false
83+
result.to_a.should eql(result.to_a)
84+
end
85+
8186
it "should yield different value for #first if streaming" do
8287
result = @client.query "SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false
8388
result.first.should_not eql(result.first)

0 commit comments

Comments
 (0)