Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 6f75e48

Browse files
committed
Merge branch 'rm/strchrnul-not-strlen'
* rm/strchrnul-not-strlen: use strchrnul() in place of strchr() and strlen()
2 parents 884377c + 2c5495f commit 6f75e48

File tree

9 files changed

+31
-54
lines changed

9 files changed

+31
-54
lines changed

archive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ static void parse_treeish_arg(const char **argv,
260260
/* Remotes are only allowed to fetch actual refs */
261261
if (remote && !remote_allow_unreachable) {
262262
char *ref = NULL;
263-
const char *colon = strchr(name, ':');
264-
int refnamelen = colon ? colon - name : strlen(name);
263+
const char *colon = strchrnul(name, ':');
264+
int refnamelen = colon - name;
265265

266266
if (!dwim_ref(name, refnamelen, sha1, &ref))
267267
die("no such ref: %.*s", refnamelen, name);

cache-tree.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
117117

118118
if (!it)
119119
return;
120-
slash = strchr(path, '/');
120+
slash = strchrnul(path, '/');
121+
namelen = slash - path;
121122
it->entry_count = -1;
122-
if (!slash) {
123+
if (!*slash) {
123124
int pos;
124-
namelen = strlen(path);
125125
pos = subtree_pos(it, path, namelen);
126126
if (0 <= pos) {
127127
cache_tree_free(&it->down[pos]->cache_tree);
@@ -139,7 +139,6 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
139139
}
140140
return;
141141
}
142-
namelen = slash - path;
143142
down = find_subtree(it, path, namelen, 0);
144143
if (down)
145144
cache_tree_invalidate_path(down->cache_tree, slash + 1);

diff.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3362,14 +3362,11 @@ static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *va
33623362
if (c != '-')
33633363
return 0;
33643364
arg++;
3365-
eq = strchr(arg, '=');
3366-
if (eq)
3367-
len = eq - arg;
3368-
else
3369-
len = strlen(arg);
3365+
eq = strchrnul(arg, '=');
3366+
len = eq - arg;
33703367
if (!len || strncmp(arg, arg_long, len))
33713368
return 0;
3372-
if (eq) {
3369+
if (*eq) {
33733370
int n;
33743371
char *end;
33753372
if (!isdigit(*++eq))

fast-import.c

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,14 +1485,11 @@ static int tree_content_set(
14851485
unsigned int i, n;
14861486
struct tree_entry *e;
14871487

1488-
slash1 = strchr(p, '/');
1489-
if (slash1)
1490-
n = slash1 - p;
1491-
else
1492-
n = strlen(p);
1488+
slash1 = strchrnul(p, '/');
1489+
n = slash1 - p;
14931490
if (!n)
14941491
die("Empty path component found in input");
1495-
if (!slash1 && !S_ISDIR(mode) && subtree)
1492+
if (!*slash1 && !S_ISDIR(mode) && subtree)
14961493
die("Non-directories cannot have subtrees");
14971494

14981495
if (!root->tree)
@@ -1501,7 +1498,7 @@ static int tree_content_set(
15011498
for (i = 0; i < t->entry_count; i++) {
15021499
e = t->entries[i];
15031500
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
1504-
if (!slash1) {
1501+
if (!*slash1) {
15051502
if (!S_ISDIR(mode)
15061503
&& e->versions[1].mode == mode
15071504
&& !hashcmp(e->versions[1].sha1, sha1))
@@ -1552,7 +1549,7 @@ static int tree_content_set(
15521549
e->versions[0].mode = 0;
15531550
hashclr(e->versions[0].sha1);
15541551
t->entries[t->entry_count++] = e;
1555-
if (slash1) {
1552+
if (*slash1) {
15561553
e->tree = new_tree_content(8);
15571554
e->versions[1].mode = S_IFDIR;
15581555
tree_content_set(e, slash1 + 1, sha1, mode, subtree);
@@ -1576,11 +1573,8 @@ static int tree_content_remove(
15761573
unsigned int i, n;
15771574
struct tree_entry *e;
15781575

1579-
slash1 = strchr(p, '/');
1580-
if (slash1)
1581-
n = slash1 - p;
1582-
else
1583-
n = strlen(p);
1576+
slash1 = strchrnul(p, '/');
1577+
n = slash1 - p;
15841578

15851579
if (!root->tree)
15861580
load_tree(root);
@@ -1594,15 +1588,15 @@ static int tree_content_remove(
15941588
for (i = 0; i < t->entry_count; i++) {
15951589
e = t->entries[i];
15961590
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
1597-
if (slash1 && !S_ISDIR(e->versions[1].mode))
1591+
if (*slash1 && !S_ISDIR(e->versions[1].mode))
15981592
/*
15991593
* If p names a file in some subdirectory, and a
16001594
* file or symlink matching the name of the
16011595
* parent directory of p exists, then p cannot
16021596
* exist and need not be deleted.
16031597
*/
16041598
return 1;
1605-
if (!slash1 || !S_ISDIR(e->versions[1].mode))
1599+
if (!*slash1 || !S_ISDIR(e->versions[1].mode))
16061600
goto del_entry;
16071601
if (!e->tree)
16081602
load_tree(e);
@@ -1644,11 +1638,8 @@ static int tree_content_get(
16441638
unsigned int i, n;
16451639
struct tree_entry *e;
16461640

1647-
slash1 = strchr(p, '/');
1648-
if (slash1)
1649-
n = slash1 - p;
1650-
else
1651-
n = strlen(p);
1641+
slash1 = strchrnul(p, '/');
1642+
n = slash1 - p;
16521643
if (!n && !allow_root)
16531644
die("Empty path component found in input");
16541645

@@ -1664,7 +1655,7 @@ static int tree_content_get(
16641655
for (i = 0; i < t->entry_count; i++) {
16651656
e = t->entries[i];
16661657
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
1667-
if (!slash1)
1658+
if (!*slash1)
16681659
goto found_entry;
16691660
if (!S_ISDIR(e->versions[1].mode))
16701661
return 0;

match-trees.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,10 @@ static int splice_tree(const unsigned char *hash1,
182182
enum object_type type;
183183
int status;
184184

185-
subpath = strchr(prefix, '/');
186-
if (!subpath)
187-
toplen = strlen(prefix);
188-
else {
189-
toplen = subpath - prefix;
185+
subpath = strchrnul(prefix, '/');
186+
toplen = subpath - prefix;
187+
if (*subpath)
190188
subpath++;
191-
}
192189

193190
buf = read_sha1_file(hash1, &type, &sz);
194191
if (!buf)
@@ -215,7 +212,7 @@ static int splice_tree(const unsigned char *hash1,
215212
if (!rewrite_here)
216213
die("entry %.*s not found in tree %s",
217214
toplen, prefix, sha1_to_hex(hash1));
218-
if (subpath) {
215+
if (*subpath) {
219216
status = splice_tree(rewrite_here, subpath, hash2, subtree);
220217
if (status)
221218
return status;

parse-options.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,10 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
223223
const struct option *options)
224224
{
225225
const struct option *all_opts = options;
226-
const char *arg_end = strchr(arg, '=');
226+
const char *arg_end = strchrnul(arg, '=');
227227
const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
228228
int abbrev_flags = 0, ambiguous_flags = 0;
229229

230-
if (!arg_end)
231-
arg_end = arg + strlen(arg);
232-
233230
for (; options->type != OPTION_END; options++) {
234231
const char *rest, *long_name = options->long_name;
235232
int flags = 0, opt_flags = 0;

pretty.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,14 +555,13 @@ static char *get_header(const struct commit *commit, const char *msg,
555555
const char *line = msg;
556556

557557
while (line) {
558-
const char *eol = strchr(line, '\n'), *next;
558+
const char *eol = strchrnul(line, '\n'), *next;
559559

560560
if (line == eol)
561561
return NULL;
562-
if (!eol) {
562+
if (!*eol) {
563563
warning("malformed commit (header is missing newline): %s",
564564
sha1_to_hex(commit->object.sha1));
565-
eol = line + strlen(line);
566565
next = NULL;
567566
} else
568567
next = eol + 1;

remote-testsvn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ static int parse_rev_note(const char *msg, struct rev_note *res)
7878
size_t len;
7979

8080
while (*msg) {
81-
end = strchr(msg, '\n');
82-
len = end ? end - msg : strlen(msg);
81+
end = strchrnul(msg, '\n');
82+
len = end - msg;
8383

8484
key = "Revision-number: ";
8585
if (starts_with(msg, key)) {

ws.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@ unsigned parse_whitespace_rule(const char *string)
3333
int negated = 0;
3434

3535
string = string + strspn(string, ", \t\n\r");
36-
ep = strchr(string, ',');
37-
if (!ep)
38-
len = strlen(string);
39-
else
40-
len = ep - string;
36+
ep = strchrnul(string, ',');
37+
len = ep - string;
4138

4239
if (*string == '-') {
4340
negated = 1;

0 commit comments

Comments
 (0)