From patchwork Sun Feb 4 05:48:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mohit Marathe X-Patchwork-Id: 13544476 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 077726FB9 for ; Sun, 4 Feb 2024 05:48:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707025724; cv=none; b=L/6A0bL9tZLEbcRH//L7AUXZJynzT8HDvAtVc9st1KqXvBR2LrcUGKEorAslNEP+xMq6MWK1Q9ARuxJa3k2f26my0NEGtT0V4WVxHaY/ilQVXDwQ9C+BfphnrkI6s/7jUm12iFN+LQ2AbYpseGBKzXmTrSq3+DOZCIWCw5Dt3j4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707025724; c=relaxed/simple; bh=t1pnBQkJl51ZJtibERqIEoF2J8lIsj9Utdt21WIxqaw=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=CthxhFUROQ+pS7yhQrJhXcvwaT9AQoewO+QlXgK5F6i/BP8KQN16pZKjigHwrD3YPB6KjnCKX0Gv44PIY6w0nXouiMrGnsuGOkyRpBbZn6btRFcmD8UReKE1Z03O/1kLWSZC0CmmgLbUyR5c7JImLVCran61tSbf4DHWkZVx9gI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fHUvMUPW; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fHUvMUPW" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-40fc65783aeso17892075e9.2 for ; Sat, 03 Feb 2024 21:48:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707025721; x=1707630521; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=ZcJxdQl0R/kYPifJMm+diLvkXJLQZJEG1pF8oQkFxvs=; b=fHUvMUPWwh/v3JgjmL2QCBD5+sZuhHum60PAiiLLrKYnIBoc3HEfZD5hw6dkQrqwm3 2g5pPkv1XoFog5OeTr7viK6cNNtRjhGu/Rja+LBbHBoEYVUxHAnu0vuZm7el3Zw9vECU 9uJ8hYA5PbvoFQeEL8Dp5Jxh6Dlm12oO/W/y81F+UEPlH9DPEpfsmattbWxmd0HBqAjg yFbH7ynYg0TQ6szmJl9QfKWHKIAR0xjk30aEg6pQMmXV1qHGuE7PRxrE225WMvXmX6Sm gdYSghebfakyEUzTGISMKJrcQWRccgOruKyHXUWy5oWvWy7DxtJQGdnJLAYeEJYPmelo 31Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707025721; x=1707630521; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZcJxdQl0R/kYPifJMm+diLvkXJLQZJEG1pF8oQkFxvs=; b=CUuKVBzvOVN6F3pOBA7TOXHbXat4LfTAGJtAeseMfWM+TXfFjsyoscR4GwWvESJ22B xG1LmtctPlpUopfxBe4VlkudZurFW6xd3nf1hBddPoEAzk/o8GPl65qPsok/Yu3/zVpv y7LXShZZqdQF4txDXPZA7KYg2f4Q/XbvW7uy1dHaj7K4tkSnLC+bVrPbGOxPKsEV3Ego XpOm9Owk/Zzarz14pI5hLn0KxB+tbFiGM+zkpW2psXiBvmJux5ADoLRA8W/8P+rSIOyO kJaC7sl43Nz8hSnFAlmzbNeMBluvxc/vzfX7fSTHqL3LLACGMY05SlZ5RBlKeJfkR5xg ThnA== X-Gm-Message-State: AOJu0Yx/kN+D9X/jN2TBszQokxbaPcEZmH+McdsRLgLckf/E2O6/nATJ Yg4g9dQUIvDlj5SY2FvSkqAb45k1fYAoutOEU1vi1M+O/1kjCaAVfqD980cF X-Google-Smtp-Source: AGHT+IF+hQzKSfGL/LLPyjJi9jq28JDn7Ts5asK7Fj7zIRE0UzkO5+A/Kh4FKaiEthIOp8K+jBf5uw== X-Received: by 2002:a05:600c:314b:b0:40f:b9e5:ab35 with SMTP id h11-20020a05600c314b00b0040fb9e5ab35mr1884050wmo.36.1707025720972; Sat, 03 Feb 2024 21:48:40 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXIY9vf9UjT+BHHWbay9urvaHBXeEmZUyZAbQsaqTQSNYTPUBvUfm+yJhlPYmp3ZhUuUI9GzaUrwZ0wxTW8AxbPSe7teIpZzOk= Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j17-20020a05600c1c1100b0040fbdd6f69bsm4770805wms.33.2024.02.03.21.48.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Feb 2024 21:48:40 -0800 (PST) Message-ID: <98e516a7be777f2697073e64b28c293e7a8a0011.1707025719.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sun, 04 Feb 2024 05:48:37 +0000 Subject: [PATCH v6 1/2] git-compat-util: add strtoi_with_tail() Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Mohit Marathe , Mohit Marathe From: Mohit Marathe From: Mohit Marathe This function is an updated version of strtol_i() function. It will give more control to handle parsing of the characters after the numbers and better error handling while parsing numbers. Signed-off-by: Mohit Marathe --- git-compat-util.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/git-compat-util.h b/git-compat-util.h index 7c2a6538e5a..59256a441de 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1296,14 +1296,24 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result) return 0; } -static inline int strtol_i(char const *s, int base, int *result) +#define strtol_i(s,b,r) strtoi_with_tail((s), (b), (r), NULL) +static inline int strtoi_with_tail(char const *s, int base, int *result, char **endp) { long ul; - char *p; + char *dummy = NULL; + if (!endp) + endp = &dummy; errno = 0; - ul = strtol(s, &p, base); - if (errno || *p || p == s || (int) ul != ul) + ul = strtol(s, endp, base); + if (errno || + /* + * if we are told to parse to the end of the string by + * passing NULL to endp, it is an error to have any + * remaining character after the digits. + */ + (dummy && *dummy) || + *endp == s || (int) ul != ul) return -1; *result = ul; return 0; From patchwork Sun Feb 4 05:48:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mohit Marathe X-Patchwork-Id: 13544477 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 65C2D6FD1 for ; Sun, 4 Feb 2024 05:48:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707025726; cv=none; b=NFHb/TUNAbEsqjLVO5EZvW2ZShmqyifvaBUkXpl1iw/R6h2GPw4XLf13V/rM0GWzvKlVmLHPOWvC5Z+oqeTQJlhwxSPFFojuIcfjpucGvRLootPTxTeuZ4d0n6jIpZbcx+YtgZkZFyghnLYmrRAX7Xx4bOvvY2Cj0LBnLtFi9HU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707025726; c=relaxed/simple; bh=7OzWWH480D9y4QRp1Ct6fFzJEblka5C8hZrO6UjedzY=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=Ni2d8731wR4psu33wc+GvuQeAZAwlhtsbPOAy2Ff4TQ0QkO64nZUXRlDsxMBNyymdWH2vFkVpeSaysRM4MaOjxqi8EBMjA5mjxZn4RqwnLu3fC5zgqHPhEywkkg0J16p1UZzh1+gvt5tAIR9JqeVoh+5RePakpeUOAmIjetOQ1s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OG+LvfzG; arc=none smtp.client-ip=209.85.221.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OG+LvfzG" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-3392b12dd21so1790067f8f.0 for ; Sat, 03 Feb 2024 21:48:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707025722; x=1707630522; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=bkl11tAQxhk6kNYb+NxUU1IaiaU4nY6Syst86cT/Yq4=; b=OG+LvfzGOBxHeXMSCSMT4lmzLjckVybHfDZiBY1OOLWYaBn9Tj/iCtvYgo0izzh7d6 Mg+lcCZG6faG5xcK5eQpiIg6yUXPZYZrz2xoQrB2Fo6BhRvpMY38OG0LxUm58+Pqkgat 5swAaEkQtnwV+wtFaikM/TaRj10jcT/zBajdJp9HvyyfqxYHhdba/uoDXcZklEv7JfBk dkce8Fc0JJYBvoGGNBRzPNcMm4r4IrD0ZgubDSDQWQXcQYUG6AmkbhV+nfwP/E/vI0Ra A0Ql0CMXRF3lXcsax8UKFkm9G2g8Q64penEnXftj0OfpRJBFyuzqEA2RumwZV4r3o93O KQEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707025722; x=1707630522; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bkl11tAQxhk6kNYb+NxUU1IaiaU4nY6Syst86cT/Yq4=; b=rrcfX2UUsvbCiC80j5ocxqLc38NRbK8UvLdQc8OMO8anvlWVBPQJNTCxhSIuhFxxJw usKM6p9MUlt7JyCu7m+FQPaA27kHRQgHjxH8xi3l82l8kB7M04cPzR1i5ubeIeSZa2t1 DcBCzk2kopHvAFZxG4wRywXtFx1IqrexEUuvb9TbeNPa9BfvrQHCPf8/DrUZ3nSWW7JE Y7UXjoT/rxQgS9NcuiXtAHX3uHukZd9qY5PFrHMw1HeVMtstQDNjSqbQ7rZIJHjf4PgV V/QzL+JYfwqOdZY5WRkb2YPxZBgR3xUMIAUaJZGxBVk02+5xMOErT8VAUn2fuZ/MRTj7 uqwA== X-Gm-Message-State: AOJu0YzIZZuIcdAr00d9nfSV8VYlgIRalpgtRBP81ZtMFGqMOCh7e29A ZKtFDOtjnAFyR04QUuiLjskIL9fC0P3CslC6AOQhYh7cGBMjr/tN8XPJ0wIU X-Google-Smtp-Source: AGHT+IEfbY4ZsuwEeiCcO+PGINTgJSaYJIT22TCyBPq9ifxExTXcWETBQYjy2ml5iCW0GFuJjAKZLg== X-Received: by 2002:adf:cb02:0:b0:33a:e70d:4fb6 with SMTP id u2-20020adfcb02000000b0033ae70d4fb6mr3480933wrh.37.1707025721714; Sat, 03 Feb 2024 21:48:41 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCU5gxRBNI/lF8plVkLl7NqUK2ue5S+xmSZq8pTopthx/CwwL9Jn5c7IBbb5+EF6iWEO3aAeh5Ok+nTBxnrFrRUHOWhrO0qz7wI= Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c6-20020a056000104600b0033b35ca71c0sm976754wrx.108.2024.02.03.21.48.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Feb 2024 21:48:41 -0800 (PST) Message-ID: <858d6f94e79ba388267cc24f43ebf68998cd56a8.1707025719.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sun, 04 Feb 2024 05:48:38 +0000 Subject: [PATCH v6 2/2] patch-id: replace `atoi()` with `strtoi_with_tail` Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Mohit Marathe , Mohit Marathe From: Mohit Marathe From: Mohit Marathe The change is made to improve the error-handling capabilities during the conversion of string to integers. The `strtoi_with_tail` function offers a more robust mechanism for converting strings to integers by providing enhanced error detection. Unlike `atoi`, `strtoi_with_tail` allows the code to differentiate between a valid conversion and an invalid one, offering better resilience against potential issues such as reading hunk header of a corrupted patch. Signed-off-by: Mohit Marathe --- builtin/patch-id.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/builtin/patch-id.c b/builtin/patch-id.c index 3894d2b9706..4e9a301e9fb 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -1,3 +1,4 @@ +#include "git-compat-util.h" #include "builtin.h" #include "config.h" #include "diff.h" @@ -29,14 +30,16 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after) { static const char digits[] = "0123456789"; const char *q, *r; + char *endp; int n; q = p + 4; n = strspn(q, digits); if (q[n] == ',') { q += n + 1; - *p_before = atoi(q); - n = strspn(q, digits); + if (strtoi_with_tail(q, 10, p_before, &endp) != 0) + return 0; + n = endp - q; } else { *p_before = 1; } @@ -48,8 +51,9 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after) n = strspn(r, digits); if (r[n] == ',') { r += n + 1; - *p_after = atoi(r); - n = strspn(r, digits); + if (strtoi_with_tail(r, 10, p_after, &endp) != 0) + return 0; + n = endp - r; } else { *p_after = 1; }