Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit a917c6a025a774b38d9487d14e3f449f7598924b 1 parent 385ff10
Aaron Patterson tenderlove authored
Showing with 13 additions and 2 deletions.
  1. +5 −2 ext/mysql2/result.c
  2. +8 −0 spec/mysql2/result_spec.rb
7 ext/mysql2/result.c
View
@@ -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 */
8 spec/mysql2/result_spec.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.