From: Libor Peltan Date: Mon, 12 Nov 2018 13:34:09 +0000 (+0100) Subject: get rid of atoi() X-Git-Tag: v2.8.0~164^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2f0e6eef031844de722471295f511db155bdb0a4;p=thirdparty%2Fknot-dns.git get rid of atoi() --- diff --git a/src/contrib/strtonum.h b/src/contrib/strtonum.h index 02ab961cf3..2dc92cbefb 100644 --- a/src/contrib/strtonum.h +++ b/src/contrib/strtonum.h @@ -112,6 +112,11 @@ inline static int str_to_u32(const char *src, uint32_t *dst) CONVERT(uint, uint32_t, 0, UINT32_MAX, src, dst); } +inline static int str_to_u64(const char *src, uint64_t *dst) +{ + CONVERT(uint, uint64_t, 0, UINT64_MAX, src, dst); +} + inline static int str_to_size(const char *src, size_t *dst, size_t min, size_t max) { CONVERT(uint, size_t, min, max, src, dst); diff --git a/src/knot/dnssec/kasp/kasp_db.c b/src/knot/dnssec/kasp/kasp_db.c index 140b54ef2e..8a76459fbd 100644 --- a/src/knot/dnssec/kasp/kasp_db.c +++ b/src/knot/dnssec/kasp/kasp_db.c @@ -22,6 +22,7 @@ #include #include "contrib/files.h" +#include "contrib/strtonum.h" #include "contrib/wire_ctx.h" #include "knot/dnssec/key_records.h" #include "knot/journal/serialization.h" @@ -203,6 +204,14 @@ static const char *key_str(const knot_db_val_t *key) return (key->data + 1 + knot_dname_size(key_dname(key))); } +// returns zero time (= infinity) if failure! +static knot_time_t key_time(const knot_db_val_t *key) +{ + uint64_t r = 0; + (void)str_to_u64(key_str(key), &r); + return r; +} + static void free_key(knot_db_val_t *key) { free(key->data); @@ -843,7 +852,7 @@ int kasp_db_load_offline_records(kasp_db_t *db, const knot_dname_t *for_dname, if ((it = db_api->iter_next(it)) != NULL && db_api->iter_key(it, &key) == KNOT_EOK) { if (key_class(&key) == KASPDBKEY_OFFLINE_RECORDS && knot_dname_cmp(key_dname(&key), r->rrsig.owner) == 0) { - *next_time = atol(key_str(&key)); + *next_time = key_time(&key); } } cleanup: @@ -871,7 +880,7 @@ int kasp_db_delete_offline_records(kasp_db_t *db, const knot_dname_t *zone, while (ret == KNOT_EOK && iter != NULL && (ret = db_api->iter_key(iter, &key)) == KNOT_EOK && key.len > TIME_STRLEN && key_class(&key) == KASPDBKEY_OFFLINE_RECORDS && - knot_time_cmp(atol(key_str(&key)), to_time) <= 0 && + knot_time_cmp(key_time(&key), to_time) <= 0 && knot_dname_cmp(key_dname(&key), zone) == 0) { ret = knot_db_lmdb_iter_del(iter); iter = db_api->iter_next(iter); diff --git a/src/knot/journal/journal.c b/src/knot/journal/journal.c index 6507e4e350..4e35d53673 100644 --- a/src/knot/journal/journal.c +++ b/src/knot/journal/journal.c @@ -597,7 +597,8 @@ static void unmake_header(const knot_db_val_t *from, uint32_t *serial_to, static int first_digit(char * of) { - return atoi(of); + unsigned maj, min; + return sscanf(of, "%u.%u", &maj, &min) == 2 ? maj : -1; } static void md_update_journal_count(txn_t * txn, int change_amount) diff --git a/src/utils/keymgr/functions.c b/src/utils/keymgr/functions.c index 89fb7de5e8..8f88cbd73e 100644 --- a/src/utils/keymgr/functions.c +++ b/src/utils/keymgr/functions.c @@ -24,6 +24,7 @@ #include "utils/keymgr/bind_privkey.h" #include "contrib/base64.h" #include "contrib/ctype.h" +#include "contrib/strtonum.h" #include "contrib/tolower.h" #include "contrib/wire_ctx.h" #include "libdnssec/error.h" @@ -123,22 +124,19 @@ static bool genkeyargs(int argc, char *argv[], bool just_timing, // parse args for (int i = 0; i < argc; i++) { if (!just_timing && strncasecmp(argv[i], "algorithm=", 10) == 0) { - if (is_digit(argv[i][10]) && atol(argv[i] + 10) < 256) { - *algorithm = atol(argv[i] + 10); - continue; - } - int al; - for (al = 0; al < 256; al++) { + int alg = 256; // invalid value + (void)str_to_int(argv[i] + 10, &alg, 0, 255); + for (int al = 0; al < 256 && alg > 255; al++) { if (algnames[al] != NULL && strcasecmp(argv[i] + 10, algnames[al]) == 0) { - *algorithm = al; - break; + alg = al; } } - if (al == 256) { + if (alg > 255) { printf("Unknown algorithm: %s\n", argv[i] + 10); return false; } + *algorithm = alg; } else if (strncasecmp(argv[i], "ksk=", 4) == 0) { bitmap_set(flags, DNSKEY_GENERATE_KSK, str2bool(argv[i] + 4)); } else if (strncasecmp(argv[i], "zsk=", 4) == 0) { @@ -147,7 +145,10 @@ static bool genkeyargs(int argc, char *argv[], bool just_timing, bitmap_set(flags, DNSKEY_GENERATE_SEP_SPEC, true); bitmap_set(flags, DNSKEY_GENERATE_SEP_ON, str2bool(argv[i] + 4)); } else if (!just_timing && strncasecmp(argv[i], "size=", 5) == 0) { - *keysize = atol(argv[i] + 5); + if (str_to_u16(argv[i] + 5, keysize) != KNOT_EOK) { + printf("Invalid size: '%s'\n", argv[i] + 5); + return false; + } } else if (!just_timing && strncasecmp(argv[i], "addtopolicy=", 12) == 0) { *addtopolicy = argv[i] + 12; } else if (!init_timestamps(argv[i], timing)) { @@ -617,20 +618,6 @@ int keymgr_generate_tsig(const char *tsig_name, const char *alg_name, int bits) return KNOT_EOK; } -static long is_uint32(const char *string) -{ - if (*string == '\0') { - return -1; - } - for (const char *p = string; *p != '\0'; p++) { - if (!is_digit(*p)) { - return -1; - } - } - long res = atol(string); - return (res <= UINT32_MAX ? res : -1); -} - static bool is_hex(const char *string) { for (const char *p = string; *p != '\0'; p++) { @@ -643,8 +630,10 @@ static bool is_hex(const char *string) int keymgr_get_key(kdnssec_ctx_t *ctx, const char *key_spec, knot_kasp_key_t **key) { - long spec_tag = is_uint32(key_spec), spec_len = strlen(key_spec); - if (spec_tag < 0 && !is_hex(key_spec)) { + uint16_t keytag; + bool has_keytag = (str_to_u16(key_spec, &keytag) == KNOT_EOK); + long spec_len = strlen(key_spec); + if (!has_keytag && !is_hex(key_spec)) { printf("Error in key specification.\n"); return KNOT_EINVAL; } @@ -652,8 +641,8 @@ int keymgr_get_key(kdnssec_ctx_t *ctx, const char *key_spec, knot_kasp_key_t **k *key = NULL; for (size_t i = 0; i < ctx->zone->num_keys; i++) { knot_kasp_key_t *candidate = &ctx->zone->keys[i]; - if ((spec_tag >= 0 && dnssec_key_get_keytag(candidate->key) == spec_tag) || - (spec_tag < 0 && strncmp(candidate->id, key_spec, spec_len) == 0)) { + if ((has_keytag && dnssec_key_get_keytag(candidate->key) == keytag) || + (!has_keytag && strncmp(candidate->id, key_spec, spec_len) == 0)) { if (*key == NULL) { *key = candidate; } diff --git a/src/utils/keymgr/main.c b/src/utils/keymgr/main.c index 10c4be89ae..3c95eef033 100644 --- a/src/utils/keymgr/main.c +++ b/src/utils/keymgr/main.c @@ -22,6 +22,7 @@ #include #include "contrib/string.h" +#include "contrib/strtonum.h" #include "knot/conf/conf.h" #include "knot/dnssec/zone-keys.h" #include "libknot/libknot.h" @@ -345,7 +346,7 @@ int main(int argc, char *argv[]) { NULL } }; - int opt = 0; + int opt = 0, parm = 0; while ((opt = getopt_long(argc, argv, "hVd:c:C:t:", opts, NULL)) != -1) { switch (opt) { case 'h': @@ -373,8 +374,10 @@ int main(int argc, char *argv[]) } break; case 't': - ret = keymgr_generate_tsig(optarg, (argc > optind ? argv[optind] : "hmac-sha256"), - (argc > optind + 1 ? atol(argv[optind + 1]) : 0)); + if (argc > optind + 1) { + (void)str_to_int(argv[optind + 1], &parm, 0, 65536); + } + ret = keymgr_generate_tsig(optarg, (argc > optind ? argv[optind] : "hmac-sha256"), parm); if (ret != KNOT_EOK) { printf("Failed to generate TSIG (%s)\n", knot_strerror(ret)); }