From patchwork Wed Jan 24 06:48:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mohit Marathe X-Patchwork-Id: 13528647 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 1C8C0168A8 for ; Wed, 24 Jan 2024 06:48:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706078890; cv=none; b=pk/4prcpKY/VRtRuVGB61NkxuA3Q0mNIoWkuvEtrdweRT5cu86QJqzO3X1zmEk1BNgag61Y2ypO4hDMLsElEQuEflyHO866tbmevJu1AO8uHKGZg6jtkBMXcdISZE+SM/2+Zb4Rt5XONk6S7iVNDj7CVeHF2h+Ngsh7b5fH366s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706078890; c=relaxed/simple; bh=cvB9/72Wxp5HfkFdJNKQXcnlmLIA3G9LNM+2P1toZUg=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=DgbvVAg+qYgPsli4TT/sOnyxxksiZrrBV2h2+Y/DvJR9hKcBkcfLeVqfEPEZYMFpXM5QvpFhEIUiVlMI/JyzRIDGRjXs1EGeuOlSUrJTI2pM/UhRel5bNOIz2kaUMPlvQVfrCKWiRhGX2AcgLo6d9VxDMxY3WFpliNGE9WeBYD8= 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=f1rU1IkV; arc=none smtp.client-ip=209.85.128.51 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="f1rU1IkV" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-40eb033c1b0so29650295e9.2 for ; Tue, 23 Jan 2024 22:48:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706078886; x=1706683686; 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=rx1AMjOq3+OH82dLgTsfAb8gURv04azI+Pk9ozmtWZ8=; b=f1rU1IkVfb2JIhQoIBbV9yym99JpYyEOH8BlAUMuS0MgK3F1TLfMhy+7+1N3meVz4y qwT3RK2ky/PeSMMHxTvHSbywzeOQMAUM9ZHfX1AjhsvvZnge70L6yxorGizYdu+N6n/R eQQkqn49ldNghJEyMjW3g9o22FT3plVvkGVawvtHqlpWkIb2rE7ocIdl+MVlqHt2kZrG Y2Qjep1h7nVY8a+ygNy95cbOxHLJH4/9/xfvTi5XFFFZ6DyTe2376tS/ix61EP37Mu5M Eu7baoI3l5uOfdKgyTA4NT3stganHJVvZVYh49JbnP2310Naf5gWD+J5vHTBC8Bxagoi bUsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706078886; x=1706683686; 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=rx1AMjOq3+OH82dLgTsfAb8gURv04azI+Pk9ozmtWZ8=; b=HiBTmyhv+wrY6ArcSncmZmV6ppkXxRblEDv4QIYQW9vCPIqxmEyRwpPot4TkNulnhJ 3s1B/G5wjFnDz4b5LTYFp/WsNX1oJXsVknu1CSTFwAGUunata6P5a7trfEOgWZKJBaCn ZTU4CgfwYrLzGXMWZTw+Zif9LyaDcMTDXcCbSLDX1CnigBoDQ9esPp7kRltSb7dTNsBC vDPjSDCQqi9rkWP3bnyRTygtvjCfFnWgorwG8jqOYDMAmHzi2EubL0wpgcgigAYUi10p qE7TUyK44ZshFfGUtkNLULzf4uhI0ztlzurG+CBgqVV3YM5IRKSer5aGzNTVBM7NqN56 aLng== X-Gm-Message-State: AOJu0YzLjTUnSjamCLXjsH6Kf4He8yN9BxagHyJytpVupjpJ/1shDbrV jw+QsgkOsKM6EywPc0zL7oK+kttQ8iXA6/xMipSox5XikLibgl+jYipGxYJe X-Google-Smtp-Source: AGHT+IE2sWum+kVhZcBg+Kw40XzOOeU1SQRZ/mvmp9aClNos9yLQUT7Cz/DVr4uRNdo2sHPleYqKwQ== X-Received: by 2002:a05:600c:5118:b0:40e:a7cf:10e0 with SMTP id o24-20020a05600c511800b0040ea7cf10e0mr858192wms.20.1706078886612; Tue, 23 Jan 2024 22:48:06 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p21-20020a05600c359500b0040e3488f16dsm44242417wmq.12.2024.01.23.22.48.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 22:48:06 -0800 (PST) Message-ID: <60ea85a701a05831b0adf1e3f9a7a97fd31ef43f.1706078885.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 24 Jan 2024 06:48:04 +0000 Subject: [PATCH v3 1/2] git-compat-util: add strtol_i_updated() 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 integer and better error handling while parsing numbers. Signed-off-by: Mohit Marathe --- git-compat-util.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/git-compat-util.h b/git-compat-util.h index 7c2a6538e5a..b38d7c7f8f1 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1309,6 +1309,29 @@ static inline int strtol_i(char const *s, int base, int *result) return 0; } +#define strtol_i(s,b,r) strtol_i_updated((s), (b), (r), NULL) +static inline int strtol_i_updated(char const *s, int base, int *result, char **endp) +{ + long ul; + char *dummy = NULL; + + if (!endp) + endp = &dummy; + errno = 0; + 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; +} + void git_stable_qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); #ifdef INTERNAL_QSORT From patchwork Wed Jan 24 06:48:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mohit Marathe X-Patchwork-Id: 13528648 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 1ED04168AC for ; Wed, 24 Jan 2024 06:48:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706078891; cv=none; b=XIgZ/gFjeY4OkVVYHI7BpvAIkbOEeDwY+DUHlFhF2hdZcp7jHz+IPoDawXBaqYYHXpYvMh75cPDdLg/pLPY3tLUzpOKJNnIQGpRt8AtVjltvDtmhqSFgZwFW4BT1jAipNYyKp8QLUXOIuxcjs4A7Ac229RhOgyH41Wan02TN8/Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706078891; c=relaxed/simple; bh=/eMyJNQksJ+WwLooeMaKwdts/3lIg2fvIcP1hG39W3c=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=gaxmWwWQ3KTrJrLIOK9V6d9lR4yoWei9dnxrHLkeA8ejChE3bHg5eboXm2lXil0+An5I5mMeGGssNW/6oGh7sT99B7kvpXmzKiTG9FPo4xYIrgcFTn6lTE0oOE74nog/3a8P8blsVB/bDbB4LXxoj3ZZI4rw4g8p4iK8rp/xNZ8= 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=UeCZZWuY; arc=none smtp.client-ip=209.85.221.46 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="UeCZZWuY" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-337d58942c9so5371316f8f.0 for ; Tue, 23 Jan 2024 22:48:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706078888; x=1706683688; 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=vaFF2W0gFQ6nyUPlu2dOTdZeGaTFx/6zN0tIlmo2TYY=; b=UeCZZWuYD9yYZBYrth9hOjO0BXnNgScNdT0e9Y0nSGzdprqba3ZM/RAdHgh2VS9+8a glcOx2ejsSo7EVTQHqPeLC65BqZa0yPWi7HjSR6n+IAoerEDZw8yug9uwz4ED6QlKNOU UfwdfIpC10TOPqf7LUwz4i8/dBchjqrA8PUuLopB7Cw1qHTFZ1cCDnagtjTj8yELDls0 9jkNt3UHr5It+C9yiJfDCN5rdJFFlJnO1SVt1CgP9c38UBbOetPWNqm2UcSALsKIs97j qPMDnWBHbeUSP8WsAayv/pTZYdlCMgiyW2sm9ZB4M77X+5zq2barHa4IAcLdim4881TL kq3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706078888; x=1706683688; 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=vaFF2W0gFQ6nyUPlu2dOTdZeGaTFx/6zN0tIlmo2TYY=; b=oZiFIycT5WJUqbvM3aCdtlsPjFTOL8/Gm6fvZuZqUPOR2vlAEQv3HRJjiz02vnkRr3 cjVcRF+/a/qOJKRZIskmpHwNdEQAETzxSuFkmaD8TgTUlnN4C92+q39V9JDOSOPWtiSL YyVdvEmgSHsNq9Di7OFCrPRKiNi1HbylfFKs4DDLOL8GzzFPrRimsMjzXflPu1M0xVT7 SZiEFm1rJAD0K3ai/uMTJmtWdsDafEkHsCgpAAhi/HDt+Ys0qTLcBQHR5QErBkJAnrvQ SfmiRxcopTmMJT+89h2KLb1AHbknxM+iyUAsnxN7lSyFnAYB7QUreTYfl2WShDA2yYJS 5CbA== X-Gm-Message-State: AOJu0YyMc2hjhmXOoVyMEpnMYYaSUeBv9AuqWTn0ygn4vw7+h3uHkBfW C5slKX5gwwPbjJudDvmd65sR7tXSyBA0Yc6eG6pe0kkroHX1hVNpfjaJQo7l X-Google-Smtp-Source: AGHT+IEvf8UtFUeYY2M1NdrV/qnmJU5f3y/aT3TYiYguD+7m4333jpju5/3mPxu8ZtR/gDlB9jQnGA== X-Received: by 2002:a05:600c:1c05:b0:40e:71e3:139d with SMTP id j5-20020a05600c1c0500b0040e71e3139dmr802661wms.154.1706078887787; Tue, 23 Jan 2024 22:48:07 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c6-20020a5d4f06000000b0033930068ca8sm8129462wru.21.2024.01.23.22.48.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 22:48:06 -0800 (PST) Message-ID: <0e117198d0144fa4ee6ccf01ec31a099eaf8121c.1706078885.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 24 Jan 2024 06:48:05 +0000 Subject: [PATCH v3 2/2] patch-id: replace `atoi()` with `strtol_i_updated()` 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 representations to integers. The `strtol_i_updated(` function offers a more robust mechanism for converting strings to integers by providing enhanced error detection. Unlike `atoi(`, `strtol_i_updated(` 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, 10 insertions(+), 2 deletions(-) diff --git a/builtin/patch-id.c b/builtin/patch-id.c index 3894d2b9706..2c00d45cb2c 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,18 @@ 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); + if (strtol_i_updated(q, 10, p_before, &endp) != 0) + return 0; n = strspn(q, digits); + if (endp != q + n) + return 0; } else { *p_before = 1; } @@ -48,8 +53,11 @@ 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); + if (strtol_i_updated(r, 10, p_after, &endp) != 0) + return 0; n = strspn(r, digits); + if (endp != q + n) + return 0; } else { *p_after = 1; }