From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id CFADA383B6E7 for ; Tue, 13 Dec 2022 19:18:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CFADA383B6E7 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-pf1-x42a.google.com with SMTP id w26so2893336pfj.6 for ; Tue, 13 Dec 2022 11:18:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=UdMdv0wiZdaO+/p0UT2l1ISmQTyym/i8jO4wB4WxUMg=; b=jU4boscqOm5LZliQW52byT2T7LYdR2PxzB2UY1H/54OZi37A3viyWkW5hhBcINSTXv cTPol7TwMBQFD6TJmCCFVhhmkwSFTXcCQ1uxuPvk/iIyIxcLiDVnfNFiH7BobKDDZ23F OpwWsoTroHYVIBVIshBJlvWla38YzOZ/Aw7fEkv+FWwDeBP2jyVvT1SbldP9zRUAWnpZ IJz+yNXybkB1IUfaxXOWQaGTPFjqAUwN+LVkNvItUMbC9j4kICDfGXarBOODabyDHqoJ GAV8PCiU3Lcy4Mg3+9bG+/akUSvEtyYtOVt/OqgHxDv6CG6rFzuHixAB2jROIMYDCCnh 0/sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UdMdv0wiZdaO+/p0UT2l1ISmQTyym/i8jO4wB4WxUMg=; b=fRVatAsEWeIxSayCuGXKbEZct3xdibc7iLQg4y2BlFkiYDuZqHx5OPtSU13DatbSrm oKTeP0XCBs3SXFATIbzF4cS9TkZGnh+U/Cvj/CweMevAFQBeUrfHX4puEC912w64gQ0f sLuuymCUbmE7NipUZ9yQvqFzWhK3wpF++702KhXmQorQlEZYff2gFX6Zlcy7v5xjN8+j nDoXGJT+FQ2LFcWhGK5wDZvfy7rNpI7O2tFjx3xhH4o6KHWmBi7Ortn7P4/MDn7lb8nX 8hzRYDkAJPBBPLETEIOPq77u3obCCA7295Zvxhu+dXySzxMb50HNlDU82i/drFMRnhPe yYyQ== X-Gm-Message-State: ANoB5pn9eEfCguJavHFFFzNPVJr7gACIAg8Pu6XMGpfhf37g+zjZOwSd Ga7DrdSIoEqOS6zOLm5GLUlq9YA6xE1QC+Sq1K0= X-Google-Smtp-Source: AA0mqf75OtIMlimz8HcZqxGySpA549E9JACI/sTnfn9/TAhd0AHOHE9m3eF9Q8NC0WRPMm84fTU3MDY8Nr3o03tYgMw= X-Received: by 2002:a05:6a00:410e:b0:578:3bad:f269 with SMTP id bu14-20020a056a00410e00b005783badf269mr915103pfb.66.1670959122792; Tue, 13 Dec 2022 11:18:42 -0800 (PST) MIME-Version: 1.0 References: <3597df95-929b-dfe2-d356-7938a99165ac@gmail.com> In-Reply-To: <3597df95-929b-dfe2-d356-7938a99165ac@gmail.com> From: Andrew Pinski Date: Tue, 13 Dec 2022 11:18:30 -0800 Message-ID: Subject: Re: [BUG] missing warning for pointer arithmetic out of bounds To: Alejandro Colomar Cc: gcc@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Tue, Dec 13, 2022 at 11:16 AM Alejandro Colomar via Gcc wrote: > > > > On 12/13/22 20:08, Alejandro Colomar wrote: > > Hi! > > > > For the following program: > > > > > > $ cat buf.c > > #include > > > > int main(void) > > { > > char *p, buf[5]; > > > > p = buf + 6; > > printf("%p\n", p); > > } > > > > > > There are no warnings in gcc, as I would expect: > > I just re-read my text, and it is ambiguous. I meant that I expect warnings. GCC only warns during VRP which is only enabled at -O2: :8:12: warning: array subscript 6 is outside array bounds of 'char[5]' [-Warray-bounds=] 8 | p = buf + 6; | ~~^~~~~~~~~ :6:19: note: at offset 6 into object 'buf' of size 5 6 | char *p, buf[5]; | ^~~ Thanks, Andrew > > > > > > $ gcc -Wall -Wextra buf.c -O0 > > > > Clang does warn, however: > > > > $ clang -Weverything -Wall -Wextra buf.c -O0 > > buf.c:8:17: warning: format specifies type 'void *' but the argument has > > type 'char *' [-Wformat-pedantic] > > printf("%p\n", p); > > ~~ ^ > > %s > > buf.c:7:6: warning: the pointer incremented by 6 refers past the end of the > > array (that contains 5 elements) [-Warray-bounds-pointer-arithmetic] > > p = buf + 6; > > ^ ~ > > buf.c:5:2: note: array 'buf' declared here > > char *p, buf[5]; > > ^ > > 2 warnings generated. > > > > Cheers, > > > > Alex > > > > > > -- >