Bug #37047 valgrind reports 2 errors from 2 contexts from libmysqlclient
Submitted: 28 May 2008 19:20 Modified: 17 Nov 2008 8:52
Reporter: notorious big Email Updates:
Status: No Feedback Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S3 (Non-critical)
Version:5.0.51a OS:Linux (Debian unstable)
Assigned to: Assigned Account CPU Architecture:Any
Tags: libmysqlclient, Memory, valgrind

[28 May 2008 19:20] notorious big
Description:
Setup:
Debian unstable
libmysqlclient15-dev 5.0.51a-6
mysql-server-5.0 5.0.51a-5

libc6-dev 2.7-10
GCC 4.2.3-4
valgrind 3.3.0-1

Valgrind command:
valgrind -v --leak-check=full test

GCC command:
gcc -o test test.c -lmysqlclient -lz -Wall -pedantic -g

valgrind summary:
==14993== IN SUMMARY: 2 errors from 2 contexts (suppressed: 42 from 1)
==14993==
==14993== malloc/free: in use at exit: 61,336 bytes in 16 blocks.
==14993== malloc/free: 73 allocs, 57 frees, 109,193 bytes allocated.
==14993==
==14993== searching for pointers to 16 not-freed blocks.
==14993== checked 621,968 bytes.
==14993==
==14993== LEAK SUMMARY:
==14993== definitely lost: 0 bytes in 0 blocks.
==14993== possibly lost: 0 bytes in 0 blocks.
==14993== still reachable: 61,336 bytes in 16 blocks.
==14993== suppressed: 0 bytes in 0 blocks.
==14993== Reachable blocks (those to which a pointer was found) are not shown.
==14993== To see them, rerun with: --leak-check=full --show-reachable=yes 

Table used:
CREATE TABLE `databasehere`.`tblErrorLog` (
`ID` INTEGER NOT NULL AUTO_INCREMENT,
`Function` VARCHAR(100) NOT NULL,
`MSG` VARCHAR(254) NOT NULL,
`Timestamp` TIMESTAMP,
PRIMARY KEY (`ID`)
)
ENGINE = MyISAM; 

How to repeat:
#include <stdio.h>
#include <mysql/mysql.h>
#include <string.h>
#include <stdlib.h>

MYSQL *conn;

char server[] = "localhost";
char user[] = "user";
char password[] = "password";
char database[] = "database";

int dbmysqlOpen()
{
conn = mysql_init(NULL);

if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
printf("dbmysql::dbmysqlOpen: %s\n", mysql_error(conn));
return 0;
}

return 1;
}

int dbmysqlClose()
{
mysql_close(conn);

return 1;
}

int dbPrintLog(char *Function, char *Message)
{
char *pBuff;
char Base[] = "INSERT INTO tblErrorLog (Function,MSG) VALUES ('", Mid[] = "','", End[] = "');";
int Len, Len1, Len2, Len3;

Len1=strlen(Function);
Len2=strlen(Message);
Len3=strlen(Base)+1;
Len=Len1+Len2+Len3+6;

pBuff=(char *) malloc(Len * sizeof(char));

strncpy(pBuff,Base,Len3);
strncat(pBuff,Function,Len1);strncat(pBuff,Mid,3);
strncat(pBuff,Message,Len2);strncat(pBuff,End,3);

printf("dbmysql::dbPrintlog->Query: %s\n",pBuff);

if (mysql_query(conn, pBuff)) {
printf("dbmysql::dbPrintLog: %s\n", mysql_error(conn));

free(pBuff);
return 0;
}

free(pBuff);

return 1;
}

int main(int argc, char **argv){
char la[]="111TESTFIE111";
char lala[]="111TTEEESSSSSTT111";

dbmysqlOpen();

dbPrintLog(la,lala);

dbmysqlClose();

return 0;
}
[12 Jun 2008 20:05] Vladimir Kirillov
have the same leak, Gentoo, mysql-5.0.60-r1
[2 Oct 2008 13:25] Andre Heine
Not the same error, but an error:

--3258-- Command line
--3258--    mysqltest
--3258-- Startup, with flags:
--3258--    -v
--3258--    --show-reachable=yes
--3258--    --leak-check=full
--3258--    --log-file-exactly=LOG
--3258-- Contents of /proc/version:
--3258--   Linux version 2.6.18.8-0.10-default (geeko@buildhost) (gcc version 4.1.2 20061115 (prerelease) (SUSE Linux)) #1 SMP Wed Jun 4 15:46:34 UTC 2008
--3258-- Arch and hwcaps: X86, x86-sse1-sse2
==3258== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 3 from 1)
--3258-- 
--3258-- supp:    3 dl-hack3
==3258== malloc/free: in use at exit: 40,896 bytes in 11 blocks.
==3258== malloc/free: 68 allocs, 57 frees, 88,728 bytes allocated.
==3258== 
==3258== searching for pointers to 11 not-freed blocks.
==3258== checked 510,584 bytes.
==3258== 
==3258== 16 bytes in 1 blocks are still reachable in loss record 1 of 2
==3258==    at 0x40235B5: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3258==    by 0x40677CC: my_malloc (in /usr/lib/libmysqlclient.so.15.0.0)
==3258==    by 0x406844D: my_error_register (in /usr/lib/libmysqlclient.so.15.0.0)
==3258==    by 0x406745F: init_client_errs (in /usr/lib/libmysqlclient.so.15.0.0)
==3258==    by 0x40638F8: mysql_server_init (in /usr/lib/libmysqlclient.so.15.0.0)
==3258==    by 0x408C121: mysql_init (in /usr/lib/libmysqlclient.so.15.0.0)
==3258==    by 0x8048705: dbmysqlOpen (mysqltest.c:15)
==3258==    by 0x8048A05: main (mysqltest.c:68)
==3258== 
==3258== 
==3258== 40,880 bytes in 10 blocks are still reachable in loss record 2 of 2
==3258==    at 0x40235B5: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3258==    by 0x406E255: my_once_alloc (in /usr/lib/libmysqlclient.so.15.0.0)
==3258==    by 0x406E99A: (within /usr/lib/libmysqlclient.so.15.0.0)
==3258==    by 0x406F3A1: (within /usr/lib/libmysqlclient.so.15.0.0)
==3258==    by 0x406F518: get_charset_by_csname (in /usr/lib/libmysqlclient.so.15.0.0)
==3258==    by 0x408B6F7: mysql_init_character_set (in /usr/lib/libmysqlclient.so.15.0.0)
==3258==    by 0x408CB2A: mysql_real_connect (in /usr/lib/libmysqlclient.so.15.0.0)
==3258==    by 0x804874F: dbmysqlOpen (mysqltest.c:17)
==3258==    by 0x8048A05: main (mysqltest.c:68)
==3258== 
==3258== LEAK SUMMARY:
==3258==    definitely lost: 0 bytes in 0 blocks.
==3258==      possibly lost: 0 bytes in 0 blocks.
==3258==    still reachable: 40,896 bytes in 11 blocks.
==3258==         suppressed: 0 bytes in 0 blocks.

uname -a

ahe@se6:~> uname -a
Linux se6 2.6.18.8-0.10-default #1 SMP Wed Jun 4 15:46:34 UTC 2008 i686 i686 i386 GNU/Linux
ahe@se6:~> mysql -V
mysql  Ver 14.12 Distrib 5.0.26, for suse-linux-gnu (i686) using readline 5.1
ahe@se6:~>
[2 Oct 2008 13:26] Andre Heine
valgrind output

Attachment: LOG (application/octet-stream, text), 7.24 KiB.

[17 Oct 2008 8:52] Sveta Smirnova
Thank you for the report.

I can not repeat described behavior. Please upgrade to current version 5.0.67 and if problem still exists try to provide more information showing problem is in MySQL code. Also if you are using Debian packages please try with our binaries available from dev.mysql.com/downloads as well.
[18 Nov 2008 0:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".