Skip to content

Commit b2981d0

Browse files
René Scharfegitster
authored andcommitted
sha1_name: pass object name length to diagnose_invalid_sha1_path()
The only caller of diagnose_invalid_sha1_path() extracts a substring from an object name by creating a NUL-terminated copy of the interesting part. Add a length parameter to the function and thus avoid the need for an allocation, thereby simplifying the code. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ea738e2 commit b2981d0

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

sha1_name.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,8 @@ int get_sha1_blob(const char *name, unsigned char *sha1)
11371137
static void diagnose_invalid_sha1_path(const char *prefix,
11381138
const char *filename,
11391139
const unsigned char *tree_sha1,
1140-
const char *object_name)
1140+
const char *object_name,
1141+
int object_name_len)
11411142
{
11421143
struct stat st;
11431144
unsigned char sha1[20];
@@ -1147,8 +1148,8 @@ static void diagnose_invalid_sha1_path(const char *prefix,
11471148
prefix = "";
11481149

11491150
if (!lstat(filename, &st))
1150-
die("Path '%s' exists on disk, but not in '%s'.",
1151-
filename, object_name);
1151+
die("Path '%s' exists on disk, but not in '%.*s'.",
1152+
filename, object_name_len, object_name);
11521153
if (errno == ENOENT || errno == ENOTDIR) {
11531154
char *fullname = xmalloc(strlen(filename)
11541155
+ strlen(prefix) + 1);
@@ -1158,16 +1159,16 @@ static void diagnose_invalid_sha1_path(const char *prefix,
11581159
if (!get_tree_entry(tree_sha1, fullname,
11591160
sha1, &mode)) {
11601161
die("Path '%s' exists, but not '%s'.\n"
1161-
"Did you mean '%s:%s' aka '%s:./%s'?",
1162+
"Did you mean '%.*s:%s' aka '%.*s:./%s'?",
11621163
fullname,
11631164
filename,
1164-
object_name,
1165+
object_name_len, object_name,
11651166
fullname,
1166-
object_name,
1167+
object_name_len, object_name,
11671168
filename);
11681169
}
1169-
die("Path '%s' does not exist in '%s'",
1170-
filename, object_name);
1170+
die("Path '%s' does not exist in '%.*s'",
1171+
filename, object_name_len, object_name);
11711172
}
11721173
}
11731174

@@ -1332,13 +1333,8 @@ static int get_sha1_with_context_1(const char *name,
13321333
}
13331334
if (*cp == ':') {
13341335
unsigned char tree_sha1[20];
1335-
char *object_name = NULL;
1336-
if (only_to_die) {
1337-
object_name = xmalloc(cp-name+1);
1338-
strncpy(object_name, name, cp-name);
1339-
object_name[cp-name] = '\0';
1340-
}
1341-
if (!get_sha1_1(name, cp-name, tree_sha1, GET_SHA1_TREEISH)) {
1336+
int len = cp - name;
1337+
if (!get_sha1_1(name, len, tree_sha1, GET_SHA1_TREEISH)) {
13421338
const char *filename = cp+1;
13431339
char *new_filename = NULL;
13441340

@@ -1348,8 +1344,8 @@ static int get_sha1_with_context_1(const char *name,
13481344
ret = get_tree_entry(tree_sha1, filename, sha1, &oc->mode);
13491345
if (ret && only_to_die) {
13501346
diagnose_invalid_sha1_path(prefix, filename,
1351-
tree_sha1, object_name);
1352-
free(object_name);
1347+
tree_sha1,
1348+
name, len);
13531349
}
13541350
hashcpy(oc->tree, tree_sha1);
13551351
strncpy(oc->path, filename,
@@ -1360,7 +1356,7 @@ static int get_sha1_with_context_1(const char *name,
13601356
return ret;
13611357
} else {
13621358
if (only_to_die)
1363-
die("Invalid object name '%s'.", object_name);
1359+
die("Invalid object name '%.*s'.", len, name);
13641360
}
13651361
}
13661362
return ret;

0 commit comments

Comments
 (0)