Skip to content

Commit 59dc0ab

Browse files
committed
Merge branch 'ua/atoi'
Replace various calls to atoi() with strtol_i() and strtoul_ui(), and add improved error handling. * ua/atoi: imap: replace atoi() with strtol_i() for UIDVALIDITY and UIDNEXT parsing merge: replace atoi() with strtol_i() for marker size validation daemon: replace atoi() with strtoul_ui() and strtol_i()
2 parents 20ab7fa + e226ba8 commit 59dc0ab

File tree

5 files changed

+58
-11
lines changed

5 files changed

+58
-11
lines changed

daemon.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "abspath.h"
55
#include "config.h"
66
#include "environment.h"
7+
#include "gettext.h"
78
#include "path.h"
89
#include "pkt-line.h"
910
#include "protocol.h"
@@ -1308,17 +1309,20 @@ int cmd_main(int argc, const char **argv)
13081309
continue;
13091310
}
13101311
if (skip_prefix(arg, "--timeout=", &v)) {
1311-
timeout = atoi(v);
1312+
if (strtoul_ui(v, 10, &timeout))
1313+
die(_("invalid timeout '%s', expecting a non-negative integer"), v);
13121314
continue;
13131315
}
13141316
if (skip_prefix(arg, "--init-timeout=", &v)) {
1315-
init_timeout = atoi(v);
1317+
if (strtoul_ui(v, 10, &init_timeout))
1318+
die(_("invalid init-timeout '%s', expecting a non-negative integer"), v);
13161319
continue;
13171320
}
13181321
if (skip_prefix(arg, "--max-connections=", &v)) {
1319-
max_connections = atoi(v);
1322+
if (strtol_i(v, 10, &max_connections))
1323+
die(_("invalid max-connections '%s', expecting an integer"), v);
13201324
if (max_connections < 0)
1321-
max_connections = 0; /* unlimited */
1325+
max_connections = 0; /* unlimited */
13221326
continue;
13231327
}
13241328
if (!strcmp(arg, "--strict-paths")) {

imap-send.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,12 +668,12 @@ static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
668668
return RESP_BAD;
669669
}
670670
if (!strcmp("UIDVALIDITY", arg)) {
671-
if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg))) {
671+
if (!(arg = next_arg(&s)) || strtol_i(arg, 10, &ctx->uidvalidity) || !ctx->uidvalidity) {
672672
fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
673673
return RESP_BAD;
674674
}
675675
} else if (!strcmp("UIDNEXT", arg)) {
676-
if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) {
676+
if (!(arg = next_arg(&s)) || strtol_i(arg, 10, &imap->uidnext) || !imap->uidnext) {
677677
fprintf(stderr, "IMAP error: malformed NEXTUID status\n");
678678
return RESP_BAD;
679679
}
@@ -686,8 +686,8 @@ static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
686686
for (; isspace((unsigned char)*p); p++);
687687
fprintf(stderr, "*** IMAP ALERT *** %s\n", p);
688688
} else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) {
689-
if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg)) ||
690-
!(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) {
689+
if (!(arg = next_arg(&s)) || strtol_i(arg, 10, &ctx->uidvalidity) || !ctx->uidvalidity ||
690+
!(arg = next_arg(&s)) || strtol_i(arg, 10, (int *)cb->ctx) || !cb->ctx) {
691691
fprintf(stderr, "IMAP error: malformed APPENDUID status\n");
692692
return RESP_BAD;
693693
}
@@ -773,7 +773,10 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
773773
if (!tcmd)
774774
return DRV_OK;
775775
} else {
776-
tag = atoi(arg);
776+
if (strtol_i(arg, 10, &tag)) {
777+
fprintf(stderr, "IMAP error: malformed tag %s\n", arg);
778+
return RESP_BAD;
779+
}
777780
for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
778781
if (cmdp->tag == tag)
779782
goto gottag;

merge-ll.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "merge-ll.h"
1616
#include "quote.h"
1717
#include "strbuf.h"
18+
#include "gettext.h"
1819

1920
struct ll_merge_driver;
2021

@@ -427,7 +428,10 @@ enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
427428
git_check_attr(istate, path, check);
428429
ll_driver_name = check->items[0].value;
429430
if (check->items[1].value) {
430-
marker_size = atoi(check->items[1].value);
431+
if (strtol_i(check->items[1].value, 10, &marker_size)) {
432+
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
433+
warning(_("invalid marker-size '%s', expecting an integer"), check->items[1].value);
434+
}
431435
if (marker_size <= 0)
432436
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
433437
}
@@ -454,7 +458,10 @@ int ll_merge_marker_size(struct index_state *istate, const char *path)
454458
check = attr_check_initl("conflict-marker-size", NULL);
455459
git_check_attr(istate, path, check);
456460
if (check->items[0].value) {
457-
marker_size = atoi(check->items[0].value);
461+
if (strtol_i(check->items[0].value, 10, &marker_size)) {
462+
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
463+
warning(_("invalid marker-size '%s', expecting an integer"), check->items[0].value);
464+
}
458465
if (marker_size <= 0)
459466
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
460467
}

t/t5570-git-daemon.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,31 @@ TEST_PASSES_SANITIZE_LEAK=true
88
. ./test-lib.sh
99

1010
. "$TEST_DIRECTORY"/lib-git-daemon.sh
11+
12+
test_expect_success 'daemon rejects invalid --init-timeout values' '
13+
for arg in "3a" "-3"
14+
do
15+
test_must_fail git daemon --init-timeout="$arg" 2>err &&
16+
test_grep "fatal: invalid init-timeout ${SQ}$arg${SQ}, expecting a non-negative integer" err ||
17+
return 1
18+
done
19+
'
20+
21+
test_expect_success 'daemon rejects invalid --timeout values' '
22+
for arg in "3a" "-3"
23+
do
24+
test_must_fail git daemon --timeout="$arg" 2>err &&
25+
test_grep "fatal: invalid timeout ${SQ}$arg${SQ}, expecting a non-negative integer" err ||
26+
return 1
27+
done
28+
'
29+
30+
test_expect_success 'daemon rejects invalid --max-connections values' '
31+
arg='3a' &&
32+
test_must_fail git daemon --max-connections=3a 2>err &&
33+
test_grep "fatal: invalid max-connections ${SQ}$arg${SQ}, expecting an integer" err
34+
'
35+
1136
start_git_daemon
1237

1338
check_verbose_connect () {

t/t6406-merge-attr.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ test_expect_success 'retry the merge with longer context' '
118118
grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
119119
'
120120

121+
test_expect_success 'invalid conflict-marker-size 3a' '
122+
cp .gitattributes .gitattributes.bak &&
123+
echo "text conflict-marker-size=3a" >>.gitattributes &&
124+
test_when_finished "mv .gitattributes.bak .gitattributes" &&
125+
git checkout -m text 2>err &&
126+
test_grep "warning: invalid marker-size ${SQ}3a${SQ}, expecting an integer" err
127+
'
128+
121129
test_expect_success 'custom merge backend' '
122130
123131
echo "* merge=union" >.gitattributes &&

0 commit comments

Comments
 (0)