From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 96CCF1F09A5 for ; Sun, 26 Jul 2026 10:13:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785060804; cv=none; b=h3DZYEQILzMi5p01BIRV8cY9p2W5MWEwUOjPbC9ZHMZP7vE5+re9GdlNVvPTnzi+SvC12azCEXP0Kh2F8dnbgxDvtMOU3P6cYtT35SOOHZKB9x46q/khxYMZReTzGKBS75YIzwLCaeEF3jHDarGvene/FCjbunJnieySn9CxM4E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785060804; c=relaxed/simple; bh=QOdOi199BViQDB2zktzRARAZBH5W7lFBr1ovz4QbkjI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nfD0KU+jnfUpt6bSD4hem87V0kPRKOApxrv/TzeJwaCBH/KmBEICF3NfI9D7zNSI282Sg5cPGHH3CJQr+j7g3Pi1+SyRLlg50J6tLF4XD7FMu8nTfypQFaGMIyrkxiBfL5eZ6T3g+liFW/srUzR26fpdXfo7cBWBrXoQnxMx5sA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=openresty.com; spf=pass smtp.mailfrom=openresty.com; dkim=pass (2048-bit key) header.d=openresty.com header.i=@openresty.com header.b=Q+tDLb/H; arc=none smtp.client-ip=209.85.216.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=openresty.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=openresty.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=openresty.com header.i=@openresty.com header.b="Q+tDLb/H" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-382ef647e20so1801272a91.1 for ; Sun, 26 Jul 2026 03:13:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openresty.com; s=google; t=1785060803; x=1785665603; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=IzYyTqubCWXIpDP8ZIyMuH6xumXv5Qqvgs5r4fwNWNM=; b=Q+tDLb/HpocUsmSxUhUDhOh67cpBs1Moe0061ZdIjhmXTK7clfBaOlN2GCr7hNjRT8 mz4qS4eo7cI0o30+7Fx30731OxfUIWSVowU2b6MhMcBM7+MefIAcJXK8YM8qpdJDBJO9 KbRytHtqjfnPgAiwTC3xqZ8PRk2Ahj/swfXLHIY9NnDfzSyFOagffak+3d1d8Mbu/93R tpAo7nG+K4T4ojYWiyll7ak4XfKvCl2WDwiY24pl7w0rOn6mQRq42XMFYpaTTq7bDezm GzllMfiokhwBI3FdwCLkeLXSBmUHSyffertgTLM3fPCy+8bXTc5y4bnW5fd9cLk+1gnc 4PtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785060803; x=1785665603; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=IzYyTqubCWXIpDP8ZIyMuH6xumXv5Qqvgs5r4fwNWNM=; b=Y+w5mrjgyzWVmLh7SRQNheM+XQUDBS9hOgtvqLCmfbLGkSO2VoXjqKK+Z6kcQnWE37 BpUnVI3Jz0In26hVrFiO22WQ52Af8ZVKXNxHzj5WB9v5up4hoHiZL2f1OYfdT8OoAZ8L NfWBbvzdl8fhlPHU78qa346e3oO74CvPv9vrOk1gqNYU7qaFHEbeKNe5BgbnqMNH3QBW nQFg2Ply4xUWDyj1O74mbfkzTxAeEHKUclyIBh+MLR4tQ449sPhUzyj9iChmzF1kmFgc D/5So4YwobpYDV4W1YOqIyyqODIFCaDWpwHR5YprEielEM+90MVYz7lC3eR741Cm7UW8 clBw== X-Forwarded-Encrypted: i=1; AHgh+RpHn8hS+O952FSaAFSyS3G8umie0/2Uz976UrUzJ8s9+3FiuZIlfwkQHSShbMppFwdK3MWp4n6xIC59abE=@vger.kernel.org X-Gm-Message-State: AOJu0Yzps8dSiyVJSPtomNf2iRiDECU2tzFOf8OAP/ngJtVIzXSWKkEi ew4rTU2TUrzGI1uwjIAnZV7iq0kkzgDBtoCTp/QfcBwOSZ18NoEGPF52XsukfbF6YQg= X-Gm-Gg: AR+sD12XbdB27wQWrhBOcW64UN1nJNRB8zKUtIFlO+/iBluWNFibLJvjKHW+nu4JvB7 hhpGiGOVQPPPJ+9hCRn1mjt0LLbufyR9A11nVsFE3CqXmGOzfMfssHC9SYOaeneGQeu1itJ6hYk /V0pntni9wHi/DfQ//+F7QQFUqIjFo1dL+nqRqVTpM8oU9ZqFHSUqAMuAbInYts/S5axFHCGT2E LYkJGgqqo9lX5kxFdm5pmPFCV6tMDpVUB2R6Dhv1NoZB3SXP5ZQUvW0EtnbRIX+MtfgT7wqy4a4 rMjkkwkplWfRE7im+BMHRdMmebZ9LCsFxXftcQm5SspcO3x+4SpZNVU+F2xIXXTf2Amr2BFjBuU CGOHROK75SUHM0+10EmY9A3DZF0aXRUA1DwnxnT0avl6TyVMqXTOWSO24WmWBkynD03psJC6TBa Tza24uCd6OzSt1aw== X-Received: by 2002:a17:90b:254c:b0:387:e0db:3d8d with SMTP id 98e67ed59e1d1-38f298859c2mr4688509a91.40.1785060803000; Sun, 26 Jul 2026 03:13:23 -0700 (PDT) Received: from integral2.. ([2402:8780:1329:2779:9db7:a8ec:2e1c:53e1]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-314bc59e9fesm17243981eec.27.2026.07.26.03.13.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Jul 2026 03:13:22 -0700 (PDT) From: Ammar Faizi To: Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Cc: Ammar Faizi , Linux Kernel Mailing List , Linux Kselftest Mailing List , LLVM Mailing List , Yichun Zhang , Alviro Iskandar Setiawan , Shuah Khan , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , gwml@gnuweeb.org Subject: [PATCH 2/4] tools/nolibc: stdlib: avoid signed overflow in abs() and friends Date: Sun, 26 Jul 2026 17:13:03 +0700 Message-Id: <20260726101306.3772237-3-ammarfaizi2@openresty.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260726101306.3772237-1-ammarfaizi2@openresty.com> References: <20260726101306.3772237-1-ammarfaizi2@openresty.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Negating the smallest negative value of a signed type overflows, which is undefined behavior. The selftests are built with: -fsanitize=undefined -fsanitize-trap=all so a caller passing INT_MIN does not merely get an unspecified answer, it dies (on both x86-64 and i386): A simple test program: printf("x = %d\n", abs(INT_MIN)); $ ./ab Illegal instruction (core dumped) (gdb) bt #0 0x0000000000401009 in main () (gdb) x/6i main 0x401000
: mov $0x80000000,%eax 0x401005 : neg %eax 0x401007 : jno 0x40100b => 0x401009 : ud2 0x40100b : push %rax 0x40100c : mov $0x80000000,%esi Negate in the corresponding unsigned type instead. The value still cannot be represented in the result type, so the minimum is returned unchanged. Cc: Yichun Zhang Cc: Alviro Iskandar Setiawan Fixes: bf5e8a78bede ("tools/nolibc: add abs() and friends") Signed-off-by: Ammar Faizi --- tools/include/nolibc/stdlib.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index 1816c2368b68..8d86044f759f 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -32,22 +32,28 @@ static __attribute__((unused)) char itoa_buffer[21]; * As much as possible, please keep functions alphabetically sorted. */ +/* + * The absolute value of the smallest negative value is not representable in + * the result type. Negate in the unsigned type so that the overflow is + * defined and return it unchanged, like the other libcs do. + */ + static __inline__ int abs(int j) { - return j >= 0 ? j : -j; + return j >= 0 ? j : (int)-(unsigned int)j; } static __inline__ long labs(long j) { - return j >= 0 ? j : -j; + return j >= 0 ? j : (long)-(unsigned long)j; } static __inline__ long long llabs(long long j) { - return j >= 0 ? j : -j; + return j >= 0 ? j : (long long)-(unsigned long long)j; } /* must be exported, as it's used by libgcc for various divide functions */ -- Ammar Faizi