From patchwork Tue Sep 24 10:13:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Martin_Storsj=C3=B6?= X-Patchwork-Id: 51794 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:ad12:0:b0:48e:c0f8:d0de with SMTP id w18csp248307vqy; Tue, 24 Sep 2024 03:13:52 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUAfTcp0STH9kr4Q3kH2UcJbIyql1ggizFl6nO22tTbXFhK2vReXH/0d3d7ohQ8zx4HV8hiIY+u/oOIbiLVg+8O@gmail.com X-Google-Smtp-Source: AGHT+IGEDE5qLZEOGTog0MAuuYCqjWCo6v63d9YjOln9W4UQuEXokkBLh7cnZ0fF09UF515wqNfN X-Received: by 2002:a2e:bcc2:0:b0:2f8:d263:d607 with SMTP id 38308e7fff4ca-2f8d263d8c7mr3439351fa.0.1727172832127; Tue, 24 Sep 2024 03:13:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1727172832; cv=none; d=google.com; s=arc-20240605; b=GDEkRZGc/LaAI5sw7ezXVlAyaBP/RcF+WCoe34y33oVat9tkpBgnIJ4sPyIjghQbCT TVOQ93tBi+aiSU1T7hPaBvcspaHJ2PC2vW4p2loF/dOIyrYkhg5OcvtNUa8pPgyHdkPC x7RGKojyE/wBTv2dt1UwwNbeWVmEgkosytKstkpB04oP1WnolIEjZ4F2BPl6N2OaxElP pdKVA+BkxHCbDikUuk2+qkzRd24sGIA3RhjtufgQvP5d3hhI2xKFW9PXRnkLptcggGxd SkrsH3zCAVn4BghRr8VsLMP+QHY2zNVUQnzAK3Y+Em9dap7wo28qoh2NJeggNNRKfefl fDbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=V+f1RiyICTOtUQrtu0aqSRxjgjpivj31jfLm5R6LfcE=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=OdGnC9oLzLtBXYkzkWtV39zf2K2OxwRc1yoKaVY4ImgMRZ4ngQIrk14HZIit98Ij41 6x83zYQI/u+Rh/THAuzPOTp+dTHmv2YCHMZTUQhCiLL7OwIuwGyKhz3eERwkQBGNEMRF mboxO32Tueo+Pk0bl/NEgMI4NzV6J0jlZOtI1nUtVaGuzyd7nitMPZ920a0bLXs3IEro fEQCem0JOWzFUleRfQEeLsJfd0cE7xRLU2/TxzBXuy8lzoi4M8OWUSWq3kwo4sJ+BNJA TJkZ+/2m2g7wmQIjvXCHw4x8wkWQH0AcDzA4G5jIg1CcRmn/rICyNLGW5QFrnKVLqjh6 67BA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@martin-st.20230601.gappssmtp.com header.s=20230601 header.b=TWQe4n5d; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dara=fail header.i=@gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 38308e7fff4ca-2f8d283bd0bsi3835651fa.160.2024.09.24.03.13.51; Tue, 24 Sep 2024 03:13:52 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@martin-st.20230601.gappssmtp.com header.s=20230601 header.b=TWQe4n5d; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dara=fail header.i=@gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 62FD568DA58; Tue, 24 Sep 2024 13:13:47 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BD73F68D9A0 for ; Tue, 24 Sep 2024 13:13:40 +0300 (EEST) Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-5365c512b00so6393263e87.3 for ; Tue, 24 Sep 2024 03:13:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=martin-st.20230601.gappssmtp.com; s=20230601; t=1727172820; x=1727777620; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=7v4IZcDXx/QZBfdx9qivLqmSzggCyJwoIR8jH2M8JRE=; b=TWQe4n5d817C4BJ/kFbjKE9vFXIBKKjSQq4R8b7IgM2Wiug2qJlu0Amb1tuP45DvpM qO9B/7EBHiqLDcozoxuXmbgCheD1/Ut8IQV+rxiKYNgoiE4u1LoHv+0ekB12eEq+xjoR pHaWeYT5VkmoSEibfSY8eAtmcYUbJ1TtdEhpx0F7823AgGOAeFNVHi+IwkLdSSCV6C+2 I52H4kx8BVRcNgS6jn96ARGsVbxabW4H8pIKfzCEB6/5/U5gv3mj8d4ZHh/wfuEsWnKa bokDrogVQP+8whd7asfW7eSRX7p7vWdsiFFSwjXLFoMhCfvymIfMZ5rfgD6A87pqjliD lkXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727172820; x=1727777620; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7v4IZcDXx/QZBfdx9qivLqmSzggCyJwoIR8jH2M8JRE=; b=r5BvgXd86HgJE9cSh5ih/TjuoP1a97z3JBDiY7QIfAYY4Rw3ASH845mcZtbhLm5bvw LiVT6sZLCXtqGnvwRQTmaXa4mgG0TPA8WCzaFp6V9YHXVGAPwUOLnoBdyFSce8+/+dER G6VT7RQMwy4YwKIG3r3OEdSHJuuDxMA6ZIUgeWGYa1h76Avhb49nVDadja6uOJUstMzZ m8mZs4Eu3m2FddYbPC/oR/dzIN9Iw6cDNJdBYQhi9ICaElvt13PG0iC+uBF0tjLPKmWO y8r1CPxGVPr3KVG6DrOTiHZeugZl13bB+utDNYScjMNizlTDe94l46i2UtVAvBHyILMk XRpg== X-Gm-Message-State: AOJu0Yz2aBF7ITYHe3Cs1LsUtKAL/nXhveH1NHZMf6Dpb/LEEn6Vogmz BZ8z0obPkTcO9AptohbifBa5n5kO9S55aR5mOb74cio/K+zfQvSy15yCi7qaC7irLhKF9MOP7+7 xWA== X-Received: by 2002:a05:6512:3187:b0:536:55a9:4b6c with SMTP id 2adb3069b0e04-536ac2e50edmr6999769e87.13.1727172819424; Tue, 24 Sep 2024 03:13:39 -0700 (PDT) Received: from localhost (dsl-tkubng21-58c01c-243.dhcp.inet.fi. [88.192.28.243]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-537a85e0cc0sm159180e87.22.2024.09.24.03.13.39 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 24 Sep 2024 03:13:39 -0700 (PDT) From: =?utf-8?q?Martin_Storsj=C3=B6?= To: ffmpeg-devel@ffmpeg.org Date: Tue, 24 Sep 2024 13:13:37 +0300 Message-Id: <20240924101337.90430-1-martin@martin.st> X-Mailer: git-send-email 2.39.5 (Apple Git-154) MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] compat: Fix the fallback definition of stdc_trailing_zeros X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: nHeiebiD4Ndy While shifting "value" to left, we would iterate through all bits of an unsigned long long, while we only expect to count through "size * CHAR_BIT" bits; instead shift bits to the right and just count the trailing zeros. This fixes fate with MSVC. --- Fixed the UB by shifting to the right instead of to the left. --- compat/stdbit/stdbit.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/compat/stdbit/stdbit.h b/compat/stdbit/stdbit.h index b434fc2357..53419cf9f9 100644 --- a/compat/stdbit/stdbit.h +++ b/compat/stdbit/stdbit.h @@ -178,11 +178,14 @@ static inline unsigned int stdc_trailing_zeros_uc(unsigned char value) static inline unsigned int __stdc_trailing_zeros(unsigned long long value, unsigned int size) { - unsigned int zeros = size * CHAR_BIT; + unsigned int zeros = 0; - while (value != 0) { - value <<= 1; - zeros--; + if (!value) + return size * CHAR_BIT; + + while ((value & 1) == 0) { + value >>= 1; + zeros++; } return zeros;