Bug #29687 mysql_stmt_store_result memory leak in libmysqld
Submitted: 10 Jul 2007 10:09 Modified: 23 Jul 2007 0:47
Reporter: Shane Bester (Platinum Quality Contributor) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Prepared statements Severity:S1 (Critical)
Version:5.1.17, 5.1.20 OS:Any
Assigned to: Alexey Botchkov CPU Architecture:Any
Tags: memory leak, mysql_stmt_store_result

[10 Jul 2007 10:09] Shane Bester
Description:
when calling mysql_stmt_store_result and there is no resultset to retrieve, a memory leak occurs.

How to repeat:
A test program:

/*
        g++ -c -Wall -I/home/sbester/server/5.1/mysql-5.1.17-beta-linux-i686-glibc23/include leaker.c
	g++ -Wall -pthread -ldl -o leaker leaker.o /home/sbester/server/5.1/mysql-5.1.17-beta-linux-i686-glibc23/lib/libmysqld.a /usr/lib/libcrypt.a /usr/lib/libz.a

*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "mysql.h"

static char *server_args[] = {
  "this_program",       /* this string is not used */
  "--basedir=/home/sbester/server/5.1/mysql-5.1.17-beta-linux-i686-glibc23/",
  "--key_buffer_size=32M"
};
static char *server_groups[] = {
  "embedded",
  "server",
  "this_program_SERVER",
  (char *)NULL
};

int main(int argc, char** argv)
{
MYSQL *m;
MYSQL_STMT *s;
char sql[]="SELECT 1 FROM dual WHERE 0=2";
if (mysql_library_init(sizeof(server_args) / sizeof(char *),server_args, server_groups))
{
   fprintf(stderr, "could not initialize MySQL library\n");
    return 1;
}
m= mysql_init(NULL);
mysql_real_connect(m,"","","","",0,NULL,0);
s= mysql_stmt_init(m);
mysql_stmt_prepare(s, sql, strlen(sql));
while(1)
{
mysql_stmt_execute(s);
mysql_stmt_store_result(s);
while (mysql_stmt_fetch(s)==0);
mysql_stmt_free_result(s);
}
mysql_stmt_close(s); return 0;
return 0;
}

Suggested fix:
don't leak.
[16 Jul 2007 15:08] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/30968

ChangeSet@1.2527, 2007-07-16 19:08:07+05:00, holyfoot@mysql.com +1 -0
  Bug #29687 mysql_stmt_store_result memory leak in libmysqld
  
  In embedded server we use result->alloc to store field data for the
  result, but we didn't clean the result->alloc if the query returned
  an empty recordset. Cleaning for the empty recordset enabled
[20 Jul 2007 23:46] Bugs System
Pushed into 5.1.21-beta
[20 Jul 2007 23:49] Bugs System
Pushed into 5.0.48
[22 Jul 2007 17:58] Paul Dubois
Noted in 5.0.48, 5.1.21 changelogs.

The mysql_stmt_store_result() C API function caused a memory leak for
empty result sets.
[22 Jul 2007 18:31] Sergei Golubchik
in the embedded server - libmysqld - only
[23 Jul 2007 0:47] Paul Dubois
Updated changelog to note embedded-server only.