Skip to contentSkip to conversation

sanitize --abbrev option for describe

PATCH New Han-Wen Nienhuys 1 participant 1 message Aug 11, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sanitize --abbrev option for describe

Hi,

the --abbrev option doesn't allow a 40 digit SHA1 output.

The following patch fixes this, and uses a IMHO more logical way to compute the abbrev parameter, so --abbrev=52 will still yield a 40 digit SHA1.

Signature · 1 line
 Han-Wen Nienhuys - [email protected] - http://www.xs4all.nl/~hanwen

commit 62ab49fb3a050cb2a4c96fa0ab8064742544369b

Author: Han-Wen Nienhuys <[email protected]>
Date:   Thu Nov 2 02:12:11 2006 +0100

use constant variable for storing "--abbrev=". Use 40 and MINIMUM_ABBREV as bounds for --abbrev= argument.

describe.c +5 -4
index ab192f8..973520c 100644
@@ -141,6 +141,7 @@ static void describe(const char *arg, in
 
 int main(int argc, char **argv)
 {
+	char const *abbrev_option = "--abbrev=";
 	int i;
 
 	for (i = 1; i < argc; i++) {
@@ -152,10 +153,10 @@ int main(int argc, char **argv)
 			all = 1;
 		else if (!strcmp(arg, "--tags"))
 			tags = 1;
-		else if (!strncmp(arg, "--abbrev=", 9)) {
-			abbrev = strtoul(arg + 9, NULL, 10);
-			if (abbrev < MINIMUM_ABBREV || 40 < abbrev)
-				abbrev = DEFAULT_ABBREV;
+		else if (!strncmp(arg, abbrev_option, strlen (abbrev_option))) {
+			abbrev = strtoul(arg + strlen (abbrev_option), NULL, 10);
+			abbrev = MAX(MINIMUM_ABBREV, abbrev);
+			abbrev = MIN(40, abbrev);
 		}
 		else
 			usage(describe_usage);