Merge branch 'ticket28856_v2'

This commit is contained in:
Nick Mathewson 2019-01-09 08:46:40 -05:00
commit f9a7701ff1
4 changed files with 16 additions and 30 deletions

View file

@ -15,6 +15,7 @@
#include "lib/string/printf.h" #include "lib/string/printf.h"
#include "lib/memarea/memarea.h" #include "lib/memarea/memarea.h"
#include "lib/crypt_ops/crypto_rsa.h" #include "lib/crypt_ops/crypto_rsa.h"
#include "lib/ctime/di_ops.h"
#include <string.h> #include <string.h>
@ -250,6 +251,16 @@ token_check_object(memarea_t *area, const char *kwd,
return tok; return tok;
} }
/** Return true iff the <b>memlen</b>-byte chunk of memory at
* <b>memlen</b> is the same length as <b>token</b>, and their
* contents are equal. */
static bool
mem_eq_token(const void *mem, size_t memlen, const char *token)
{
size_t len = strlen(token);
return memlen == len && fast_memeq(mem, token, len);
}
/** Helper function: read the next token from *s, advance *s to the end of the /** Helper function: read the next token from *s, advance *s to the end of the
* token, and return the parsed token. Parse *<b>s</b> according to the list * token, and return the parsed token. Parse *<b>s</b> according to the list
* of tokens in <b>table</b>. * of tokens in <b>table</b>.
@ -289,7 +300,7 @@ get_next_token(memarea_t *area,
next = find_whitespace_eos(*s, eol); next = find_whitespace_eos(*s, eol);
if (!strcmp_len(*s, "opt", next-*s)) { if (mem_eq_token(*s, next-*s, "opt")) {
/* Skip past an "opt" at the start of the line. */ /* Skip past an "opt" at the start of the line. */
*s = eat_whitespace_eos_no_nl(next, eol); *s = eat_whitespace_eos_no_nl(next, eol);
next = find_whitespace_eos(*s, eol); next = find_whitespace_eos(*s, eol);
@ -300,7 +311,7 @@ get_next_token(memarea_t *area,
/* Search the table for the appropriate entry. (I tried a binary search /* Search the table for the appropriate entry. (I tried a binary search
* instead, but it wasn't any faster.) */ * instead, but it wasn't any faster.) */
for (i = 0; table[i].t ; ++i) { for (i = 0; table[i].t ; ++i) {
if (!strcmp_len(*s, table[i].t, next-*s)) { if (mem_eq_token(*s, next-*s, table[i].t)) {
/* We've found the keyword. */ /* We've found the keyword. */
kwd = table[i].t; kwd = table[i].t;
tok->tp = table[i].v; tok->tp = table[i].v;
@ -352,7 +363,7 @@ get_next_token(memarea_t *area,
goto check_object; goto check_object;
if (eol - *s <= 16 || memchr(*s+11,'\0',eol-*s-16) || /* no short lines, */ if (eol - *s <= 16 || memchr(*s+11,'\0',eol-*s-16) || /* no short lines, */
strcmp_len(eol-5, "-----", 5) || /* nuls or invalid endings */ !mem_eq_token(eol-5, 5, "-----") || /* nuls or invalid endings */
(eol-*s) > MAX_UNPARSED_OBJECT_SIZE) { /* name too long */ (eol-*s) > MAX_UNPARSED_OBJECT_SIZE) { /* name too long */
RET_ERR("Malformed object: bad begin line"); RET_ERR("Malformed object: bad begin line");
} }
@ -371,8 +382,8 @@ get_next_token(memarea_t *area,
eol = eos; eol = eos;
/* Validate the ending tag, which should be 9 + NAME + 5 + eol */ /* Validate the ending tag, which should be 9 + NAME + 5 + eol */
if ((size_t)(eol-next) != 9+obname_len+5 || if ((size_t)(eol-next) != 9+obname_len+5 ||
strcmp_len(next+9, tok->object_type, obname_len) || !mem_eq_token(next+9, obname_len, tok->object_type) ||
strcmp_len(eol-5, "-----", 5)) { !mem_eq_token(eol-5, 5, "-----")) {
tor_snprintf(ebuf, sizeof(ebuf), "Malformed object: mismatched end tag %s", tor_snprintf(ebuf, sizeof(ebuf), "Malformed object: mismatched end tag %s",
tok->object_type); tok->object_type);
ebuf[sizeof(ebuf)-1] = '\0'; ebuf[sizeof(ebuf)-1] = '\0';

View file

@ -212,21 +212,6 @@ strcmpstart(const char *s1, const char *s2)
return strncmp(s1, s2, n); return strncmp(s1, s2, n);
} }
/** Compare the s1_len-byte string <b>s1</b> with <b>s2</b>,
* without depending on a terminating nul in s1. Sorting order is first by
* length, then lexically; return values are as for strcmp.
*/
int
strcmp_len(const char *s1, const char *s2, size_t s1_len)
{
size_t s2_len = strlen(s2);
if (s1_len < s2_len)
return -1;
if (s1_len > s2_len)
return 1;
return fast_memcmp(s1, s2, s2_len);
}
/** Compares the first strlen(s2) characters of s1 with s2. Returns as for /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
* strcasecmp. * strcasecmp.
*/ */

View file

@ -33,7 +33,6 @@ int tor_strisnonupper(const char *s);
int tor_strisspace(const char *s); int tor_strisspace(const char *s);
int strcmp_opt(const char *s1, const char *s2); int strcmp_opt(const char *s1, const char *s2);
int strcmpstart(const char *s1, const char *s2); int strcmpstart(const char *s1, const char *s2);
int strcmp_len(const char *s1, const char *s2, size_t len);
int strcasecmpstart(const char *s1, const char *s2); int strcasecmpstart(const char *s1, const char *s2);
int strcmpend(const char *s1, const char *s2); int strcmpend(const char *s1, const char *s2);
int strcasecmpend(const char *s1, const char *s2); int strcasecmpend(const char *s1, const char *s2);

View file

@ -2153,15 +2153,6 @@ test_util_strmisc(void *arg)
tt_int_op(strcmp_opt(NULL, "foo"), OP_LT, 0); tt_int_op(strcmp_opt(NULL, "foo"), OP_LT, 0);
tt_int_op(strcmp_opt("foo", NULL), OP_GT, 0); tt_int_op(strcmp_opt("foo", NULL), OP_GT, 0);
/* Test strcmp_len */
tt_int_op(strcmp_len("foo", "bar", 3), OP_GT, 0);
tt_int_op(strcmp_len("foo", "bar", 2), OP_LT, 0);
tt_int_op(strcmp_len("foo2", "foo1", 4), OP_GT, 0);
tt_int_op(strcmp_len("foo2", "foo1", 3), OP_LT, 0); /* Really stop at len */
tt_int_op(strcmp_len("foo2", "foo", 3), OP_EQ, 0); /* Really stop at len */
tt_int_op(strcmp_len("blah", "", 4), OP_GT, 0);
tt_int_op(strcmp_len("blah", "", 0), OP_EQ, 0);
done: done:
tor_free(cp_tmp); tor_free(cp_tmp);
} }