Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1beMH0-0005RK-Rb for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Aug 2016 13:06:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1beMGz-0003VV-K0 for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Aug 2016 13:06:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1beMGy-0003VO-LP for pgsql-hackers@postgresql.org; Mon, 29 Aug 2016 13:06:44 +0000 Received: from newmail.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1beMGr-0000KS-Jw for pgsql-hackers@postgresql.org; Mon, 29 Aug 2016 13:06:44 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 1719E21C2484; Mon, 29 Aug 2016 16:06:36 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru Received: from mail.postgrespro.ru ([127.0.0.1]) by localhost (mail.postgrespro.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id kGt0nMxSixFx; Mon, 29 Aug 2016 16:06:36 +0300 (MSK) Received: from localhost (unknown [192.168.27.1]) by mail.postgrespro.ru (Postfix) with ESMTPSA id BC66621C2482; Mon, 29 Aug 2016 16:06:35 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1472475995; bh=oV/5d8F/VP8nt8k60iKMmZFwsAjktBgTHXgGDoGSvlk=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=YMuSbY2/PbOy8yddnL/8cEqjDD9Zq61vCaT7dNCw8aeFFhtAq+OUittyPhlCj2oUN UJT5AM8d7j+RlpfKu4Hb4rHlLYt3s6AErqRVtLwfmixY3aRsBUQ9krekIs2LzTJk7G usYgJwhsX6Hmk1uhbUfzPb8w82ncpWTMwxPwPxe8= Date: Mon, 29 Aug 2016 16:05:14 +0300 From: Aleksander Alekseev To: Michael Paquier Cc: Heikki Linnakangas , Tom Lane , PostgreSQL mailing lists Subject: Re: Missing checks when malloc returns NULL... Message-ID: <20160829130513.GA8340@e733> References: <7978.1466516782@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="rwEMma7ioTxnRzrJ" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.6.1 (2016-04-27) X-Pg-Spam-Score: -3.5 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --rwEMma7ioTxnRzrJ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hello, Michael > I don't know how you did it, but this email has visibly broken the > original thread. Did you change the topic name? I'm very sorry for this. I had no local copy of this thread. So I wrote a new email with the same subject hoping it will be OK. Apparently right In-Reply-To header is also required. > if (prodesc->user_proname == NULL || prodesc->internal_proname == NULL) > + { > + free(prodesc); I think that prodesc->user_proname and prodesc->internal_proname should also be freed if they are not NULL's. > By the way maybe someone knows other procedures besides malloc, realloc > and strdup that require special attention? I recalled that there is also calloc(). I've found four places that use calloc() and look suspicious to me (see attachment). What do you think - are these bugs or not? -- Best regards, Aleksander Alekseev --rwEMma7ioTxnRzrJ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="calloc.txt" Looks like resource leak: ./src/backend/storage/buffer/localbuf.c: LocalBufferDescriptors = (BufferDesc *) calloc(nbufs, sizeof(BufferDesc)); ./src/backend/storage/buffer/localbuf.c: LocalBufferBlockPointers = (Block *) calloc(nbufs, sizeof(Block)); ./src/backend/storage/buffer/localbuf.c: LocalRefCount = (int32 *) calloc(nbufs, sizeof(int32)); ./src/backend/storage/buffer/localbuf.c- if (!LocalBufferDescriptors || !LocalBufferBlockPointers || !LocalRefCount) ./src/backend/storage/buffer/localbuf.c- ereport(FATAL, ./src/backend/storage/buffer/localbuf.c- (errcode(ERRCODE_OUT_OF_MEMORY), ./src/backend/storage/buffer/localbuf.c- errmsg("out of memory"))); ./src/backend/storage/buffer/localbuf.c- ./src/interfaces/libpq/fe-print.c- nTups = PQntuples(res); ./src/interfaces/libpq/fe-print.c: if (!(fieldNames = (const char **) calloc(nFields, sizeof(char *)))) ./src/interfaces/libpq/fe-print.c- { ./src/interfaces/libpq/fe-print.c- fprintf(stderr, libpq_gettext("out of memory\n")); ./src/interfaces/libpq/fe-print.c- abort(); ./src/interfaces/libpq/fe-print.c- } ./src/interfaces/libpq/fe-print.c: if (!(fieldNotNum = (unsigned char *) calloc(nFields, 1))) ./src/interfaces/libpq/fe-print.c- { ./src/interfaces/libpq/fe-print.c- fprintf(stderr, libpq_gettext("out of memory\n")); ./src/interfaces/libpq/fe-print.c- abort(); ./src/interfaces/libpq/fe-print.c- } ./src/interfaces/libpq/fe-print.c: if (!(fieldMax = (int *) calloc(nFields, sizeof(int)))) ./src/interfaces/libpq/fe-print.c- { ./src/interfaces/libpq/fe-print.c- fprintf(stderr, libpq_gettext("out of memory\n")); ./src/interfaces/libpq/fe-print.c- abort(); ./src/interfaces/libpq/fe-print.c- } Two identical pieces of code: ./src/backend/bootstrap/bootstrap.c- scan = heap_beginscan_catalog(rel, 0, NULL); ./src/backend/bootstrap/bootstrap.c- i = 0; ./src/backend/bootstrap/bootstrap.c- while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL) ./src/backend/bootstrap/bootstrap.c- ++i; ./src/backend/bootstrap/bootstrap.c- heap_endscan(scan); ./src/backend/bootstrap/bootstrap.c: app = Typ = ALLOC(struct typmap *, i + 1); ./src/backend/bootstrap/bootstrap.c- while (i-- > 0) ./src/backend/bootstrap/bootstrap.c: *app++ = ALLOC(struct typmap, 1); ./src/backend/bootstrap/bootstrap.c- *app = NULL; ./src/backend/bootstrap/bootstrap.c- scan = heap_beginscan_catalog(rel, 0, NULL); ./src/backend/bootstrap/bootstrap.c- app = Typ; ./src/backend/bootstrap/bootstrap.c- while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL) ./src/backend/bootstrap/bootstrap.c- { ./src/backend/bootstrap/bootstrap.c- scan = heap_beginscan_catalog(rel, 0, NULL); ./src/backend/bootstrap/bootstrap.c- i = 0; ./src/backend/bootstrap/bootstrap.c- while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL) ./src/backend/bootstrap/bootstrap.c- ++i; ./src/backend/bootstrap/bootstrap.c- heap_endscan(scan); ./src/backend/bootstrap/bootstrap.c: app = Typ = ALLOC(struct typmap *, i + 1); ./src/backend/bootstrap/bootstrap.c- while (i-- > 0) ./src/backend/bootstrap/bootstrap.c: *app++ = ALLOC(struct typmap, 1); ./src/backend/bootstrap/bootstrap.c- *app = NULL; ./src/backend/bootstrap/bootstrap.c- scan = heap_beginscan_catalog(rel, 0, NULL); ./src/backend/bootstrap/bootstrap.c- app = Typ; ./src/backend/bootstrap/bootstrap.c- while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL) ./src/backend/bootstrap/bootstrap.c- { --rwEMma7ioTxnRzrJ Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --rwEMma7ioTxnRzrJ--