From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id 838083858C31 for ; Sun, 26 Feb 2023 17:53:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 838083858C31 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-x432.google.com with SMTP id e37so1525389wri.10 for ; Sun, 26 Feb 2023 09:53:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=RfQ4KPS0B8dQYHVrL/1S8Qi9mH3Qa2+4Atu6nowpXkM=; b=PS52wpEyKf0S2M/lFxPowfXtECv+DE07usDV05oYFgYz2LHvwcuqawcgEdcijZ5jzu VXNg2eVBVZvfWWRDJUHoGR1PZ9mkyFBkEnKtaqdVMylvypa8z08ELoGi23Zr2Dagsufo CgfIoELQ9vC/m6HVc0RVDCv+JwMot6i1VPHmggKbMlTdmiT3d6z/uzmsflFxxVbUcqA2 +dctQDi3nKOh2F5tUPf1EwJuXCvegJacKMV6x3JOxrKBfw9Fa3OZL6SB8HONh6sAZFHN ZttNppORPSYhPn3ImKbq11j+ftLD4qB6mMy5OdfMny+Y3FJLdhRIv4QwwlW1eBtNxfVj p+ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=RfQ4KPS0B8dQYHVrL/1S8Qi9mH3Qa2+4Atu6nowpXkM=; b=0l4rCV26WJR+j+3GhLZal77GTMX1E+t/BwxtK5mmj/Pj/vW50VeZjBEnLnjwxjLydG B83S/StSnid5z1FjDOlMiyXrMaTHnIpi60lM/S4PPbTHNR8NyHKbrCeAySrEHZPoFSQ6 5OairqXR70hR+IofmS9J05sAo3lkBXmfdd+aqG3DjedrIu3w2rE1TKiyv3Kv5D3ZhNEU MocctmLDFRBvFNofoLJkSbRrfp+YKjc4n6ZKZDrhF5dMi+ec/N+BexU/vVDrXFmx4yak L1MXh/GsoSlSa3Ue04+M3W1ZOEZk9qcWSw/Zbm68wU6E5zf0cD0P5l6CifIcnASI4Jog msBw== X-Gm-Message-State: AO0yUKVBlxVhAZ/x6/UgvJCFpAEcvDtsYwwkZq3hvZMycl5lxH+MviIp 7CyW9XfR0aseTjlBp3FrQGJHdL5VIekOiQBGiGe3NGNRmww= X-Google-Smtp-Source: AK7set953F6b728J9XqTApVAD61vKz/O2P2w1I297K/zOZ2cuCuisrTihOkCTn3SqoYreomYWXWuDgiwItVjMXjkjqU= X-Received: by 2002:a5d:4d10:0:b0:2cb:80af:e8ab with SMTP id z16-20020a5d4d10000000b002cb80afe8abmr112243wrt.11.1677433984044; Sun, 26 Feb 2023 09:53:04 -0800 (PST) MIME-Version: 1.0 From: Martin Georgiev Date: Sun, 26 Feb 2023 19:52:53 +0200 Message-ID: Subject: -fanalyzer false positive on gcc 12.1 and gcc 12.2 To: gcc-help@gcc.gnu.org Content-Type: multipart/alternative; boundary="000000000000c0cfed05f59e08cd" X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE,KAM_NUMSUBJECT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000c0cfed05f59e08cd Content-Type: text/plain; charset="UTF-8" While compiling a source file with the -fanalyzer option I got a compiler -Wanalyser-malloc-leak warning. I followed the diagnostic message but didn't see the problem so I decided to go look at the assembly code. Compiling the same source file with gcc 11 and gcc trunk generated identical assembly code but I did not get the error message. Is this a gcc 12 bug or am I missing something? You can recreate the behavior described above by switching between different gcc versions on godbolt.org. #include #include #include struct Binary_Tree_Node { struct Binary_Tree_Node *left; struct Binary_Tree_Node *right; struct Binary_Tree_Node *parent; void *key; }; typedef struct{ struct Binary_Tree_Node *root; int (*comp)(const void *, const void *); void (*key_destr)(void *); size_t size; } Binary_Tree; struct AVL_Node { struct Binary_Tree_Node node; int balance; }; typedef struct { Binary_Tree tree; } AVL_Tree; enum cdsa_err {ERR_OK = 0, ERR_OUT_OF_MEM, ERR_ELEMENT_NOT_FOUND, ERR_ELEMENT_EXISTS, ERR_TREE_IS_FULL}; __attribute__((nonnull(1), access(read_write, 1), access(read_only, 2))) extern enum cdsa_err avl_insert(AVL_Tree * const t, void * const key); extern enum cdsa_err avl_insert(AVL_Tree * const t, void * const key) { Binary_Tree * const bt = (Binary_Tree * const)t; if (bt->size == SIZE_MAX) return ERR_TREE_IS_FULL; struct Binary_Tree_Node **bn = &(bt->root); struct Binary_Tree_Node *parent = NULL; // Removing this traversal loop which shouldn't have any effect removes the diagnose. while (*bn) { const int res = bt->comp(key, (*bn)->key); if (!res) return ERR_ELEMENT_EXISTS; parent = *bn; if (res < 0) { bn = &(*bn)->left; continue; } bn = &(*bn)->right; } struct AVL_Node **n = (struct AVL_Node **)bn; *n = malloc(sizeof(**n)); if (!(*n)) return ERR_OUT_OF_MEM; (*bn)->key = key; (*bn)->left = NULL; (*bn)->right = NULL; (*bn)->parent = parent; (*n)->balance = 0; //balance_insert(t, *n); ++(bt->size); return ERR_OK; } Here is the output: : In function 'avl_insert': :58:15: warning: leak of '' [CWE-401] [-Wanalyzer-malloc-leak] 58 | if (!(*n)) | ~^~~ 'avl_insert': events 1-8 | | 38 | if (bt->size == SIZE_MAX) | | ^ | | | | | (1) following 'false' branch... |...... | 41 | struct Binary_Tree_Node **bn = &(bt->root); | | ~~ | | | | | (2) ...to here |...... | 44 | while (*bn) { | | ~ | | | | | (3) following 'true' branch... | 45 | const int res = bt->comp(key, (*bn)->key); | | ~~~~~~~~ | | | | | (4) ...to here | 46 | if (!res) | | ~ | | | | | (5) following 'false' branch (when 'res != 0')... | 47 | return ERR_ELEMENT_EXISTS; | 48 | parent = *bn; | | ~~~~~~~~~~~~ | | | | | (6) ...to here |...... | 57 | *n = malloc(sizeof(**n)); | | ~~~~~~~~~~~~~~~~~~~ | | | | | (7) allocated here | 58 | if (!(*n)) | | ~~~~ | | | | | (8) '' leaks here; was allocated at (7) --000000000000c0cfed05f59e08cd--