microsoft / msphpsql

Microsoft Drivers for PHP for SQL Server
MIT License
1.78k stars 371 forks source link

Memory leak in Microsoft ODBC Driver 17.10.5 for SQL Server #1488

Open SakiTakamachi opened 7 months ago

SakiTakamachi commented 7 months ago

This is a report regarding a bug in the odbc driver. I'm writing here because I don't know the appropriate place to report it.

(I also posted it here. https://learn.microsoft.com/en-us/answers/questions/1418766/memory-leak-in-microsoft-odbc-driver-17-10-5-for-s)

The following code:

// odbc.c
#include <sql.h>
#include <sqlext.h>
#include <string.h>

int main(void) {
    HENV env;
    HDBC db;
    char *dsn = "Driver={ODBC Driver 17 for SQL Server};Server=sql-server;Port=1433;Database=test;uid=SA;pwd={/* pass */};"; // myenv
    SQLCHAR dsnbuf[1024];
    short dsnbuflen;

    SQLAllocEnv(&env);
    SQLAllocConnect(env, &db);
    SQLDriverConnect(db, NULL, (SQLCHAR*) dsn, strlen(dsn), dsnbuf, sizeof(dsnbuf) - 1, &dsnbuflen, SQL_DRIVER_NOPROMPT);

    SQLDisconnect(db);
    SQLFreeHandle(SQL_HANDLE_DBC, db);
    db = NULL;

    SQLFreeHandle(SQL_HANDLE_ENV, env);
    env = NULL;

    SQLAllocEnv(&env);
    SQLAllocConnect(env, &db);
    SQLDriverConnect(db, NULL, (SQLCHAR*) dsn, strlen(dsn), dsnbuf, sizeof(dsnbuf) - 1, &dsnbuflen, SQL_DRIVER_NOPROMPT);

    return 0;
}

compile command:

gcc -fsanitize=leak,undefined,address -fno-sanitize-recover -DZEND_TRACK_ARENA_ALLOC -Wall -o odbc odbc.c -lodbc

exec:

# ./odbc

=================================================================
==12092==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x7fd55793e587 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cc:104
    #1 0x7fd5532728da  (/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.5.1+0x1808da)

Indirect leak of 88 byte(s) in 1 object(s) allocated from:
    #0 0x7fd55793e587 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cc:104
    #1 0x7fd5531fa909  (/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.5.1+0x108909)

SUMMARY: AddressSanitizer: 112 byte(s) leaked in 2 allocation(s).

In this way, once I close the connection and connect it again, I will get a memory leak.

Regards.

v-makouz commented 7 months ago

Which operating system are you running this on, and what's the version of unixODBC?

SakiTakamachi commented 7 months ago

@v-makouz

ubuntu:20.04 (docker for mac)

# odbcinst --version
unixODBC 2.3.6

Regards.

v-makouz commented 7 months ago

I can reproduce the leak, and it looks like it was fixed in ODBC Driver 18. If possible, I would recommend using Driver 18, if not the next release of Driver 17 (with the fix) would probably be next year.

SakiTakamachi commented 7 months ago

Thank you!