From patchwork Wed Jan 24 06:55:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mohit Marathe X-Patchwork-Id: 13528654 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 D422516415 for ; Wed, 24 Jan 2024 06:55:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706079310; cv=none; b=iPBpaAUSa2pE33QU3bfIv0mTKp+AHjris+z8bjuwB2AdHGSaDq6CQaA34WBY6Qm+LMIBqXMHHOiYj3QdKDcdgNDj6vEvCefb/J7383TB7NsBvPlD6mtJ20t0eCSFHXR8TMe6gIU5wON0zsTAQo8/A6xNXTc//Phtx6aw0Tt2Vng= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706079310; c=relaxed/simple; bh=cvB9/72Wxp5HfkFdJNKQXcnlmLIA3G9LNM+2P1toZUg=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=G/l6ie5f3HTSCFhMsv1LzCvpacchAn0UltsH/iSMlAdj80of1K0zvO89a3wP0dsRqhOgIWfgY8vsHG0DZCsIasqXBCjWP/+C7PNcx4OQC1hevsRUUeb/9Q3PmxrOcGj7BbZM8LXdHQIH2b6GGWTLIgzQ1Thj1MpyCet4W+ePrtA= 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=iPL0vYmF; arc=none smtp.client-ip=209.85.128.41 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="iPL0vYmF" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-40ec3535225so9550345e9.2 for ; Tue, 23 Jan 2024 22:55:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706079306; x=1706684106; 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=iPL0vYmFQvgWn7ECe5DSBqmd8Z8zN90+WUR8FOuRobn31QArx2vhgqxuuC6K9bdB1V 0MmroC6OWZ0D+uD163wiL9/ESBiyB1A3U91J4spzPFaD8p4r55WQKJNOQ4TP8LqDtIrx ffTBX2AQ+1daUlycear5Q626ehClXxSEJ3nZ4Q9xJCwJhXsU7lItoZVJ1qNXZcUNBbz8 lfNFy/fB5VaF/VXdPP6klu2la3hwAyWbxeXE0nVmLoCk1fDq1hSKLSXCk37PY4zL7oaM WPdFZ5P/V8mB2v8Bp8pf1fVjNU4bH2kT0geqmyV3kaREzQ7qKQ29z+ePpe4MmUCYiPxP 8Cqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706079306; x=1706684106; 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=J4WQ9JpFaUMV29Vy0V8+fnahTVeZA+S5ldbep5PtmNjXRBvBQ/foPsXcmJVbPteMPd O+gZJzQmD+7u7tcRCv+ZGzzgVxcRhm/3m14cj+gv8Ju7jlbe1KKtiGqTiSfTxJXLiVSX bSdKten99dzm4CC3lqWJ22iu3GDJ/Xhm/5TA9ZjW9vQ6J8S8/3VUTjJ5tPZ7Mh6jHr0C 4DXNiaseGdi1JtwIDiijmGKjqN9LL2TmivM62TnCD/BK2UY4UpcHFr+b/nrjh325h51Z hKoQ5XeSdzoaXVM/JJIk38eNG28Q9uBT7AME7sWEMl+vAoSX//iwNb0/4OK4MuKK3/9y BgEA== X-Gm-Message-State: AOJu0Yyg+4i76bHcB/NfBAKKJFkocgPqe9N4XPVPXfvALF5YbVFCcC9p Y+pNsHFfUOx2EU9dcD2pXB/nGDe3HfIbat6vckt1inxwqBsVj3GY5EUpS1NL X-Google-Smtp-Source: AGHT+IHXvw/XxdmoMGzN4/9aFxZZtNTGZj2qHhG1UoKZD+7QTXNm4imcdJ6knbTSO9pAspvPW+UW4A== X-Received: by 2002:a05:600c:3ba2:b0:40e:a328:25ff with SMTP id n34-20020a05600c3ba200b0040ea32825ffmr644728wms.93.1706079306515; Tue, 23 Jan 2024 22:55:06 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m36-20020a05600c3b2400b0040ebfbff33csm3564213wms.36.2024.01.23.22.55.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 22:55:05 -0800 (PST) Message-ID: <60ea85a701a05831b0adf1e3f9a7a97fd31ef43f.1706079304.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 24 Jan 2024 06:55:03 +0000 Subject: [PATCH v4 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:55: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: 13528655 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 2C1B91643A for ; Wed, 24 Jan 2024 06:55:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706079311; cv=none; b=V0i0eURzcZBjO3xYTyycUIoSFKfXthA+1rhPTDn3dBPYBU8Ktzhdwvv3a8grEJOCfLQy2X2Y4ozhWGWEXZeSjDwNK92MVYCrcKKgESspWvCRBZ89nslXT1YJz7CoX3YLSm2ggOrvZ9MJ7UpgKhiMLvQhCMEXY5Q3U+094L5YdTY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706079311; c=relaxed/simple; bh=wFqnypwvu7JwUKxBJeE8hsJKjQheP3szGhpdUxNGD6g=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=iN7krj3Rn9AxJ7voRjsAXsnDsPr6yHk/vB+DWGd+OGTX5jO9yWL3XOXLEjxV3VfDz0z4SoH2PavANQDjsFFEtWmnL8CmsAhKrkjscKis2XPIHNXDFZglyjjNjy3iOF9CU5CpRpipZMl4Amof+ysABrUOM5Q2KJ16zCi1rs1QUi8= 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=FslE9ps/; arc=none smtp.client-ip=209.85.128.52 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="FslE9ps/" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-40e76109cdeso56829385e9.0 for ; Tue, 23 Jan 2024 22:55:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706079307; x=1706684107; 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=hQ8nN93BjzrXAhTOlt++q7zDpixQGOdFTMnqvbtbopM=; b=FslE9ps/PZpJdi1fJWlX9vby+jgTvhCdP+p7terZ1KgOVjG0A5yoAWohGdxlMcTb2A 8pihs3ceN/7QohVeLPHnC5CDUy4Gz8qL/CrfZFhmfr2Mic1rxqeYCkF+zUjp3xkgpl7V q1A8YwSTxYiUtFiAuhqoCqBMJvGb4FX9dW+fk7I0N4j4G8kTECrCWPIoRYMp6lu/J5gb SBVOeHJ7teAuxev23Df5G0XjYEga8Q1ZDDalv+9Wliw4wb+GQOz7x4S6/upiN5PadMGQ DIdBUyajPaUnWAFZ1F5KEtSOgtXvovBcRPEpi1dbYXacVG+JFwT/sY1wq7Zg61crsT3X cOhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706079308; x=1706684108; 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=hQ8nN93BjzrXAhTOlt++q7zDpixQGOdFTMnqvbtbopM=; b=V1InONMNd1gGOE/3Tyl4MLxFgrQnh/Gga01cGfn2JsSe1HV7phYcV3KjyQqtfUVCsy ZH2yaKWczCsAvIbdu6IvQ4Ui4oHMdR+0qWiRhKw5hGOGPwVXsTwj96M7jDVT7lgeEf7A V0zxaqeIe++yhYPdqY/DUoNd+Otdz/K4RDe3FoXpCrkGNW0lioNMukWN0+mTb9ioQB4n l5fAfYsi5Ewa56UomKzR9hSsESjjuHStEtOCcBQKkuAgymZ5N8qGdK/XARAo3YViAlSt bo/Y5U+OuANOTlCARdhcZTdwLD0LTrts+k77M+VSMN/1Ps/lJaG+uhVsN/MIvyksDJzm 57cw== X-Gm-Message-State: AOJu0YxhT1vIPD2T4pWoumEA7DAXjp8sTO4fclV9hMs0DFOQ2PSgqZqd jRtB9kIjpBQAnUxomr/h0qls3HVhMKn+KK1ybTU+UFK0v4z3QB0xXhlcsWWd X-Google-Smtp-Source: AGHT+IEPPd73NewdcBIu672efQUdMvD0Vm+gj/kuJRZ1R7yrDGZUzkkjKSOZzbXs4sUtzPx++3lh6w== X-Received: by 2002:a05:600c:4f0e:b0:40e:c31b:388e with SMTP id l14-20020a05600c4f0e00b0040ec31b388emr731941wmq.31.1706079307546; Tue, 23 Jan 2024 22:55:07 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p12-20020a05600c468c00b0040e56830a35sm44658316wmo.18.2024.01.23.22.55.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 22:55:06 -0800 (PST) Message-ID: <17f2dda4907ec03b0783160c53c4896fd76cb053.1706079304.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 24 Jan 2024 06:55:04 +0000 Subject: [PATCH v4 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..88db178c905 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 != r + n) + return 0; } else { *p_after = 1; }