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()
This commit is contained in:
Taylor Blau
2024-11-01 12:53:26 -04:00
5 changed files with 58 additions and 11 deletions

View File

@ -4,6 +4,7 @@
#include "abspath.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
#include "path.h"
#include "pkt-line.h"
#include "protocol.h"
@ -1308,17 +1309,20 @@ int cmd_main(int argc, const char **argv)
continue;
}
if (skip_prefix(arg, "--timeout=", &v)) {
timeout = atoi(v);
if (strtoul_ui(v, 10, &timeout))
die(_("invalid timeout '%s', expecting a non-negative integer"), v);
continue;
}
if (skip_prefix(arg, "--init-timeout=", &v)) {
init_timeout = atoi(v);
if (strtoul_ui(v, 10, &init_timeout))
die(_("invalid init-timeout '%s', expecting a non-negative integer"), v);
continue;
}
if (skip_prefix(arg, "--max-connections=", &v)) {
max_connections = atoi(v);
if (strtol_i(v, 10, &max_connections))
die(_("invalid max-connections '%s', expecting an integer"), v);
if (max_connections < 0)
max_connections = 0; /* unlimited */
max_connections = 0; /* unlimited */
continue;
}
if (!strcmp(arg, "--strict-paths")) {

View File

@ -668,12 +668,12 @@ static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
return RESP_BAD;
}
if (!strcmp("UIDVALIDITY", arg)) {
if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg))) {
if (!(arg = next_arg(&s)) || strtol_i(arg, 10, &ctx->uidvalidity) || !ctx->uidvalidity) {
fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
return RESP_BAD;
}
} else if (!strcmp("UIDNEXT", arg)) {
if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) {
if (!(arg = next_arg(&s)) || strtol_i(arg, 10, &imap->uidnext) || !imap->uidnext) {
fprintf(stderr, "IMAP error: malformed NEXTUID status\n");
return RESP_BAD;
}
@ -686,8 +686,8 @@ static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
for (; isspace((unsigned char)*p); p++);
fprintf(stderr, "*** IMAP ALERT *** %s\n", p);
} else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) {
if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg)) ||
!(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) {
if (!(arg = next_arg(&s)) || strtol_i(arg, 10, &ctx->uidvalidity) || !ctx->uidvalidity ||
!(arg = next_arg(&s)) || strtol_i(arg, 10, (int *)cb->ctx) || !cb->ctx) {
fprintf(stderr, "IMAP error: malformed APPENDUID status\n");
return RESP_BAD;
}
@ -773,7 +773,10 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
if (!tcmd)
return DRV_OK;
} else {
tag = atoi(arg);
if (strtol_i(arg, 10, &tag)) {
fprintf(stderr, "IMAP error: malformed tag %s\n", arg);
return RESP_BAD;
}
for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
if (cmdp->tag == tag)
goto gottag;

View File

@ -15,6 +15,7 @@
#include "merge-ll.h"
#include "quote.h"
#include "strbuf.h"
#include "gettext.h"
struct ll_merge_driver;
@ -427,7 +428,10 @@ enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
git_check_attr(istate, path, check);
ll_driver_name = check->items[0].value;
if (check->items[1].value) {
marker_size = atoi(check->items[1].value);
if (strtol_i(check->items[1].value, 10, &marker_size)) {
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
warning(_("invalid marker-size '%s', expecting an integer"), check->items[1].value);
}
if (marker_size <= 0)
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
}
@ -454,7 +458,10 @@ int ll_merge_marker_size(struct index_state *istate, const char *path)
check = attr_check_initl("conflict-marker-size", NULL);
git_check_attr(istate, path, check);
if (check->items[0].value) {
marker_size = atoi(check->items[0].value);
if (strtol_i(check->items[0].value, 10, &marker_size)) {
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
warning(_("invalid marker-size '%s', expecting an integer"), check->items[0].value);
}
if (marker_size <= 0)
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
}

View File

@ -8,6 +8,31 @@ TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-git-daemon.sh
test_expect_success 'daemon rejects invalid --init-timeout values' '
for arg in "3a" "-3"
do
test_must_fail git daemon --init-timeout="$arg" 2>err &&
test_grep "fatal: invalid init-timeout ${SQ}$arg${SQ}, expecting a non-negative integer" err ||
return 1
done
'
test_expect_success 'daemon rejects invalid --timeout values' '
for arg in "3a" "-3"
do
test_must_fail git daemon --timeout="$arg" 2>err &&
test_grep "fatal: invalid timeout ${SQ}$arg${SQ}, expecting a non-negative integer" err ||
return 1
done
'
test_expect_success 'daemon rejects invalid --max-connections values' '
arg='3a' &&
test_must_fail git daemon --max-connections=3a 2>err &&
test_grep "fatal: invalid max-connections ${SQ}$arg${SQ}, expecting an integer" err
'
start_git_daemon
check_verbose_connect () {

View File

@ -118,6 +118,14 @@ test_expect_success 'retry the merge with longer context' '
grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
'
test_expect_success 'invalid conflict-marker-size 3a' '
cp .gitattributes .gitattributes.bak &&
echo "text conflict-marker-size=3a" >>.gitattributes &&
test_when_finished "mv .gitattributes.bak .gitattributes" &&
git checkout -m text 2>err &&
test_grep "warning: invalid marker-size ${SQ}3a${SQ}, expecting an integer" err
'
test_expect_success 'custom merge backend' '
echo "* merge=union" >.gitattributes &&