0

Overview

I have a C program using libpq to send PostgreSQL queries to a database.

Despite getting the desired output I'm experiencing a memory leak related to PQconnectdb() that I cannot seem to resolve using the documentation.

The only related thing I could find is this thread that suggests its related to openSSL but I could not resolve it with their suggestion.

Test File

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libpq-fe.h>

void exit_nicely(PGconn *conn){
    fprintf(stderr, "%s\n", PQerrorMessage(conn));
    PQfinish(conn);
    exit(1);
}

int main(int argc, char **argv){
    
    // Open connection with database.
    char conninfo[100] = "<HOST NAME>";
    strcat(conninfo, "<USERNAME>");
    strcat(conninfo, "<PASSWORD>");
    PGconn *conn = PQconnectdb(conninfo);
    (PQstatus(conn) == CONNECTION_OK) ? printf("Connected\n") : exit_nicely(conn);
    
    // Send query, receive result.
    PGresult* res = PQexec(conn, "SELECT VERSION();");

    // Error checking.
    if(PQresultStatus(res) != PGRES_TUPLES_OK){
        fprintf(stderr, "ERROR%d: %s\n", PQresultStatus(res), PQresStatus(PQresultStatus(res)));
        PQclear(res);
        exit_nicely(conn);
    }
    
    // Extract data from result.
    printf("Attribute1 name: %-15s\n", PQfname(res, 0));
    printf("Attribute1, row1 value: %-15s\n", PQgetvalue(res, 0, 0));
    PQclear(res); 
 
    PQfinish(conn);
    return 0;
}

Output

valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all -s ./test
==7827== Memcheck, a memory error detector
==7827== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7827== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==7827== Command: ./test
==7827== 
Connected
Attribute1 name: version        
Attribute1, row1 value: PostgreSQL 14.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
==7827== 
==7827== HEAP SUMMARY:
==7827==     in use at exit: 153 bytes in 4 blocks
==7827==   total heap usage: 33,923 allocs, 33,919 frees, 3,248,431 bytes allocated
==7827== 
==7827== 8 bytes in 1 blocks are still reachable in loss record 1 of 4
==7827==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==7827==    by 0x4FEC4E9: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x50038A6: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x50043D9: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FDE532: gss_add_cred_from (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FE1187: gss_acquire_cred_from (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FE13C9: gss_acquire_cred (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4878B73: PQconnectPoll (in /usr/lib/x86_64-linux-gnu/libpq.so.5.15)
==7827==    by 0x487A2EF: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.15)
==7827==    by 0x487A3E7: PQconnectdb (in /usr/lib/x86_64-linux-gnu/libpq.so.5.15)
==7827==    by 0x10949F: main (in /home/shawn/Lab4/test)
==7827== 
==7827== 24 bytes in 1 blocks are still reachable in loss record 2 of 4
==7827==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==7827==    by 0x4FEC494: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x50038A6: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x50043D9: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FDE532: gss_add_cred_from (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FE1187: gss_acquire_cred_from (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FE13C9: gss_acquire_cred (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4878B73: PQconnectPoll (in /usr/lib/x86_64-linux-gnu/libpq.so.5.15)
==7827==    by 0x487A2EF: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.15)
==7827==    by 0x487A3E7: PQconnectdb (in /usr/lib/x86_64-linux-gnu/libpq.so.5.15)
==7827==    by 0x10949F: main (in /home/shawn/Lab4/test)
==7827== 
==7827== 48 bytes in 1 blocks are still reachable in loss record 3 of 4
==7827==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==7827==    by 0x517F6C4: krb5int_setspecific (in /usr/lib/x86_64-linux-gnu/libkrb5support.so.0.1)
==7827==    by 0x4FEC505: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x50038A6: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x50043D9: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FDE532: gss_add_cred_from (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FE1187: gss_acquire_cred_from (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FE13C9: gss_acquire_cred (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4878B73: PQconnectPoll (in /usr/lib/x86_64-linux-gnu/libpq.so.5.15)
==7827==    by 0x487A2EF: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.15)
==7827==    by 0x487A3E7: PQconnectdb (in /usr/lib/x86_64-linux-gnu/libpq.so.5.15)
==7827==    by 0x10949F: main (in /home/shawn/Lab4/test)
==7827== 
==7827== 73 bytes in 1 blocks are still reachable in loss record 4 of 4
==7827==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==7827==    by 0x496060E: strdup (strdup.c:42)
==7827==    by 0x4FEC456: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x50038A6: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x50043D9: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FDE532: gss_add_cred_from (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FE1187: gss_acquire_cred_from (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x5006629: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x5006749: ??? (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FDE532: gss_add_cred_from (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FE1187: gss_acquire_cred_from (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827==    by 0x4FE13C9: gss_acquire_cred (in /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2.2)
==7827== 
==7827== LEAK SUMMARY:
==7827==    definitely lost: 0 bytes in 0 blocks
==7827==    indirectly lost: 0 bytes in 0 blocks
==7827==      possibly lost: 0 bytes in 0 blocks
==7827==    still reachable: 153 bytes in 4 blocks
==7827==         suppressed: 0 bytes in 0 blocks
==7827== 
==7827== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  • 2
    The e-mail you link to explains it pretty well, doesn't it? – Laurenz Albe Nov 29 '22 at 20:54
  • 1
    The blocks allocated are still reachable. That (probably, IMO) means that the PQ library still has a use for the memory. It would be nice if it was cleaned up, but only by examining the code in the PQ library will you be able to determine whether there's a way to free the remnant blocks. It is unlikely (again, IMO) that there's a problem in your code. And, as noted by @LaurenzAlbe, the email you link to explains that the problem is with OpenSSL doing the caching, rather than PQ itself. – Jonathan Leffler Nov 29 '22 at 20:55
  • 2
    It looks like the leak is coming from kerberos, not from postgres, I'm afraid you'll have to live with it [or create a suppression file](https://stackoverflow.com/questions/17159578/generating-suppressions-for-memory-leaks) – David Ranieri Nov 29 '22 at 20:58
  • Can you compile with `gcc -fsanitize=address -fno-omit-frame-pointer` and run it outside Valgrind? – David Ranieri Nov 29 '22 at 21:06

0 Answers0