From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id BF38D385AC19 for ; Thu, 2 Sep 2021 04:03:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BF38D385AC19 Received: by mail-pf1-x433.google.com with SMTP id y17so581896pfl.13 for ; Wed, 01 Sep 2021 21:03:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition; bh=ZbbUlrlRcStC75TXUeZqigzl47jraY4L7TpRfS71FUQ=; b=blOm9PvfmaVvq7U3YiozETQteBxqDa29VBLy2Y++YxOUEikH5MuvT2iLe4/0AOHwVj lvx5dF0SezU1jkXs7xM4ebU602KmU89MDz6ct4NXNz8XuhRxLf8JVa64nzndSSXrqXaF s7rot9PTJnngIDU5IuTTs8YFmjSnFjNTwaKz1y65aT2s6OR+mxgRWIH/nlwM/IZEXrJz QWvX/P4ecJhDyx2NSyYYt3bRJzS5PBVqaFTwaCdJL0i2IGbw/k4vZIeCpOaXLy9Tt2nf LLPiLBZrDs3BRDKbRhC/EkUxIuglDEbX6S2UyfGSBsXLq2UjP56D1RbYYEPLfKtvSbW2 /feQ== X-Gm-Message-State: AOAM531WsmOLd1FiE/08wIAikBirjt1Qt0odypeK6c6o+qJP2OPud8Xx inbYq9uGVOf6jkf/4DGKvsnPlzePEyXpiQ== X-Google-Smtp-Source: ABdhPJyCcjf6bZ4ZQeWBwWKf81g2ZEtOzT+CFJA4pn4s4vNH6n0Tas98obP9LRHynRNE37gO7+TQ6Q== X-Received: by 2002:a63:1b53:: with SMTP id b19mr1183096pgm.160.1630555430863; Wed, 01 Sep 2021 21:03:50 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:60e0:7096:2da2:848]) by smtp.gmail.com with ESMTPSA id g17sm437504pfi.168.2021.09.01.21.03.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Sep 2021 21:03:50 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 785DD11402BD; Thu, 2 Sep 2021 13:33:47 +0930 (ACST) Date: Thu, 2 Sep 2021 13:33:47 +0930 From: Alan Modra To: binutils@sourceware.org, gcc-patches@gcc.gnu.org Subject: obstack.h __PTR_ALIGN vs. ubsan Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3038.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Sep 2021 04:03:53 -0000 Current ubsan complains on every use of __PTR_ALIGN (when ptrdiff_t is as large as a pointer), due to making calculations relative to a NULL pointer. This patch avoids the problem by extracting out and simplifying __BPTR_ALIGN for the usual case. I've continued to use ptrdiff_t here, where it might be better to throw away __BPTR_ALIGN entirely and just assume uintptr_t exists. OK to apply for gcc? * obstack.h (__PTR_ALIGN): Expand and simplify __BPTR_ALIGN rather than calculating relative to a NULL pointer. diff --git a/include/obstack.h b/include/obstack.h index a6eb6c95587..0d8746f835b 100644 --- a/include/obstack.h +++ b/include/obstack.h @@ -137,9 +137,9 @@ relative to B. Otherwise, use the faster strategy of computing the alignment relative to 0. */ -#define __PTR_ALIGN(B, P, A) \ - __BPTR_ALIGN (sizeof (ptrdiff_t) < sizeof (void *) ? (B) : (char *) 0, \ - P, A) +#define __PTR_ALIGN(B, P, A) \ + (sizeof (ptrdiff_t) < sizeof (void *) ? __BPTR_ALIGN (B, P, A) \ + : (char *) (((ptrdiff_t) (P) + (A)) & ~(A))) #ifndef __attribute_pure__ # if defined __GNUC_MINOR__ && __GNUC__ * 1000 + __GNUC_MINOR__ >= 2096 -- Alan Modra Australia Development Lab, IBM