Skip to content

Commit 8a6444d

Browse files
committed
Merge branch 'rs/simple-cleanups'
Code cleanups. * rs/simple-cleanups: sha1_name: use strlcpy() to copy strings pretty: use starts_with() to check for a prefix for-each-ref: use skip_prefix() to avoid duplicate string comparison connect: use strcmp() for string comparison
2 parents ca70473 + 2ce63e9 commit 8a6444d

File tree

4 files changed

+7
-13
lines changed

4 files changed

+7
-13
lines changed

builtin/for-each-ref.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,10 @@ static const char *find_next(const char *cp)
178178
static int verify_format(const char *format)
179179
{
180180
const char *cp, *sp;
181-
static const char color_reset[] = "color:reset";
182181

183182
need_color_reset_at_eol = 0;
184183
for (cp = format; *cp && (sp = find_next(cp)); ) {
185-
const char *ep = strchr(sp, ')');
184+
const char *color, *ep = strchr(sp, ')');
186185
int at;
187186

188187
if (!ep)
@@ -191,8 +190,8 @@ static int verify_format(const char *format)
191190
at = parse_atom(sp + 2, ep);
192191
cp = ep + 1;
193192

194-
if (starts_with(used_atom[at], "color:"))
195-
need_color_reset_at_eol = !!strcmp(used_atom[at], color_reset);
193+
if (skip_prefix(used_atom[at], "color:", &color))
194+
need_color_reset_at_eol = !!strcmp(color, "reset");
196195
}
197196
return 0;
198197
}

connect.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
157157
server_capabilities = xstrdup(name + name_len + 1);
158158
}
159159

160-
if (extra_have &&
161-
name_len == 5 && !memcmp(".have", name, 5)) {
160+
if (extra_have && !strcmp(name, ".have")) {
162161
sha1_array_append(extra_have, old_sha1);
163162
continue;
164163
}

pretty.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ static char *replace_encoding_header(char *buf, const char *encoding)
567567
char *cp = buf;
568568

569569
/* guess if there is an encoding header before a \n\n */
570-
while (strncmp(cp, "encoding ", strlen("encoding "))) {
570+
while (!starts_with(cp, "encoding ")) {
571571
cp = strchr(cp, '\n');
572572
if (!cp || *++cp == '\n')
573573
return buf;

sha1_name.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -1391,9 +1391,7 @@ static int get_sha1_with_context_1(const char *name,
13911391
namelen = strlen(cp);
13921392
}
13931393

1394-
strncpy(oc->path, cp,
1395-
sizeof(oc->path));
1396-
oc->path[sizeof(oc->path)-1] = '\0';
1394+
strlcpy(oc->path, cp, sizeof(oc->path));
13971395

13981396
if (!active_cache)
13991397
read_cache();
@@ -1443,9 +1441,7 @@ static int get_sha1_with_context_1(const char *name,
14431441
name, len);
14441442
}
14451443
hashcpy(oc->tree, tree_sha1);
1446-
strncpy(oc->path, filename,
1447-
sizeof(oc->path));
1448-
oc->path[sizeof(oc->path)-1] = '\0';
1444+
strlcpy(oc->path, filename, sizeof(oc->path));
14491445

14501446
free(new_filename);
14511447
return ret;

0 commit comments

Comments
 (0)