commit c6d03cf5d51c4d8b5d9ed43883f520f1e575733c from: Omar Polo date: Sun Apr 25 16:49:28 2021 UTC switch from strtok to strsep and while there fix the wrong args to strtonum commit - ec1fa0b0da154b298ffc00a08526894f2d1f8ef0 commit + c6d03cf5d51c4d8b5d9ed43883f520f1e575733c blob - 621124461c99bd05185e3c57e702f13921ee5b2d blob + 3959ee825b9e66fc3f65c56fdece058323e8080d --- fs.c +++ fs.c @@ -261,13 +261,26 @@ fs_main(struct imsgbuf *b) } + +static int +parse_khost_line(char *line, char *tmp[3]) +{ + char **ap; + + for (ap = tmp; ap < &tmp[3] && + (*ap = strsep(&line, " \t\n")) != NULL;) { + if (**ap != '\0') + ap++; + } + return ap == &tmp[3] && *line == '\0'; +} + int load_certs(struct ohash *h) { - char *p, *last, *el, *line = NULL; + char *tmp[3], *line = NULL; const char *errstr; - int i; size_t lineno = 0, linesize = 0; ssize_t linelen; FILE *f; @@ -281,41 +294,22 @@ load_certs(struct ohash *h) abort(); lineno++; - i = 0; - for ((p = strtok_r(line, " ", &last)); p; - (p = strtok_r(NULL, " ", &last))) { - if (*p == '\n') - break; - switch (i++) { - case 0: - strlcpy(e->domain, p, sizeof(e->domain)); - break; - case 1: - strlcpy(e->hash, p, sizeof(e->hash)); - break; - case 2: - if ((el = strchr(p, '\n')) == NULL) - break; - *el = '\0'; + if (parse_khost_line(line, tmp)) { + strlcpy(e->domain, tmp[0], sizeof(e->domain)); + strlcpy(e->hash, tmp[1], sizeof(e->hash)); - /* 0 <= verified <= 1 */ - e->verified = strtonum(p, -1, 2, &errstr); - if (errstr != NULL) - errx(1, "verification for %s is %s: %s", - e->domain, errstr, p); - break; - } - } - - if (i != 0 && i != 3) + e->verified = strtonum(tmp[2], 0, 1, &errstr); + if (errstr != NULL) + errx(1, "%s:%zu verification for %s is %s: %s", + known_hosts_file, lineno, + e->domain, errstr, tmp[2]); + tofu_add(h, e); + } else { warnx("%s:%zu invalid entry", known_hosts_file, lineno); - - if (i == 3) - tofu_add(h, e); - else free(e); + } } free(line);