From patchwork Sun Sep 1 18:06:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Iooss X-Patchwork-Id: 11125609 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4E3A917E0 for ; Sun, 1 Sep 2019 18:06:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2AAD12190F for ; Sun, 1 Sep 2019 18:06:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729064AbfIASGz (ORCPT ); Sun, 1 Sep 2019 14:06:55 -0400 Received: from mx1.polytechnique.org ([129.104.30.34]:42406 "EHLO mx1.polytechnique.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729036AbfIASGz (ORCPT ); Sun, 1 Sep 2019 14:06:55 -0400 Received: from localhost.localdomain (85-168-38-217.rev.numericable.fr [85.168.38.217]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ssl.polytechnique.org (Postfix) with ESMTPSA id 26CE7564880 for ; Sun, 1 Sep 2019 20:06:53 +0200 (CEST) From: Nicolas Iooss To: selinux@vger.kernel.org Subject: [PATCH 2/9] libsepol/cil: help static analyzers by aborting when an allocation fails Date: Sun, 1 Sep 2019 20:06:29 +0200 Message-Id: <20190901180636.31586-3-nicolas.iooss@m4x.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190901180636.31586-1-nicolas.iooss@m4x.org> References: <20190901180636.31586-1-nicolas.iooss@m4x.org> MIME-Version: 1.0 X-AV-Checked: ClamAV using ClamSMTP at svoboda.polytechnique.org (Sun Sep 1 20:06:53 2019 +0200 (CEST)) X-Spam-Flag: No, tests=bogofilter, spamicity=0.000000, queueID=4D800564884 X-Org-Mail: nicolas.iooss.2010@polytechnique.org Sender: selinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org When allocating memory with cil_* helpers, if malloc/calloc/realloc/... failed, (*cil_mem_error_handler)() is called. Implementations of this function are expected not to return to the caller, and the default one calls exit(1) to ensure this. In order for static analyzers to find out that cil_malloc/cil_realloc/... never returns a NULL pointer when failing to allocate some memory, introduce a call to abort(). This decreases the number of false positive warnings about null pointer dereferences reported by Infer static analyzer. Signed-off-by: Nicolas Iooss --- libsepol/cil/src/cil_mem.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libsepol/cil/src/cil_mem.c b/libsepol/cil/src/cil_mem.c index 12c59be21914..885431d8a8fd 100644 --- a/libsepol/cil/src/cil_mem.c +++ b/libsepol/cil/src/cil_mem.c @@ -55,6 +55,7 @@ void *cil_malloc(size_t size) return NULL; } (*cil_mem_error_handler)(); + abort(); } return mem; @@ -65,6 +66,7 @@ void *cil_calloc(size_t num_elements, size_t element_size) void *mem = calloc(num_elements, element_size); if (mem == NULL){ (*cil_mem_error_handler)(); + abort(); } return mem; @@ -78,6 +80,7 @@ void *cil_realloc(void *ptr, size_t size) return NULL; } (*cil_mem_error_handler)(); + abort(); } return mem; @@ -95,6 +98,7 @@ char *cil_strdup(const char *str) mem = strdup(str); if (mem == NULL) { (*cil_mem_error_handler)(); + abort(); } return mem; @@ -111,6 +115,7 @@ __attribute__ ((format (printf, 2, 3))) int cil_asprintf(char **strp, const char if (rc == -1) { (*cil_mem_error_handler)(); + abort(); } return rc;