Permalink
Browse files

the result struct should not be accessed after it's been freed.

Iterating through the rows will cause the result struct to be freed,
which means that the `count` method could do an invalid memory access.

fixes #190
  • Loading branch information...
1 parent 385ff10 commit a917c6a025a774b38d9487d14e3f449f7598924b @tenderlove tenderlove committed Mar 28, 2012
Showing with 13 additions and 2 deletions.
  1. +5 −2 ext/mysql2/result.c
  2. +8 −0 spec/mysql2/result_spec.rb
View
7 ext/mysql2/result.c
@@ -536,8 +536,11 @@ static VALUE rb_mysql_result_count(VALUE self) {
mysql2_result_wrapper *wrapper;
GetMysql2Result(self, wrapper);
-
- return INT2FIX(mysql_num_rows(wrapper->result));
+ if(wrapper->resultFreed) {
+ return LONG2NUM(RARRAY_LEN(wrapper->rows));
+ } else {
+ return INT2FIX(mysql_num_rows(wrapper->result));
+ }
}
/* Mysql2::Result */
View
8 spec/mysql2/result_spec.rb
@@ -10,6 +10,14 @@
@result = @client.query "SELECT 1"
end
+ it "should maintain a count while streaming" do
+ result = @client.query('SELECT 1')
+
+ result.count.should eql(1)
+ result.each { |r| }
+ result.count.should eql(1)
+ end
+
it "should have included Enumerable" do
Mysql2::Result.ancestors.include?(Enumerable).should be_true
end

0 comments on commit a917c6a

Please sign in to comment.