introduce hex2chr() for converting two hexadecimal digits to a character

Add and use a helper function that decodes the char value of two hexadecimal digits. It returns a negative number on error, avoids running over the end of the given string and doesn't shift negative values. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Sep 3, 2016 at 17:59 UTC d23309733a5b2a9e1adc304ee50c5a5ed7a087c2
6 files changed +21 -78
cache.h
+10
@@ -1133,6 +1133,16 @@ static inline unsigned int hexval(unsigned char c)
1133 return hexval_table[c];
1134 }
1135
1136 +/*
1137 + * Convert two consecutive hexadecimal digits into a char. Return a
1138 + * negative value on error. Don't run over the end of short strings.
1139 + */
1140 +static inline int hex2chr(const char *s)
1141 +{
1142 + int val = hexval(s[0]);
1143 + return (val < 0) ? val : (val << 4) | hexval(s[1]);
1144 +}
1145 +
1146 /* Convert to/from hex/sha1 representation */
1147 #define MINIMUM_ABBREV minimum_abbrev
1148 #define DEFAULT_ABBREV default_abbrev
hex.c
+2 -10
@@ -39,16 +39,8 @@ int get_sha1_hex(const char *hex, unsigned char *sha1)
39 {
40 int i;
41 for (i = 0; i < GIT_SHA1_RAWSZ; i++) {
42 - unsigned int val;
43 - /*
44 - * hex[1]=='\0' is caught when val is checked below,
45 - * but if hex[0] is NUL we have to avoid reading
46 - * past the end of the string:
47 - */
48 - if (!hex[0])
49 - return -1;
50 - val = (hexval(hex[0]) << 4) | hexval(hex[1]);
51 - if (val & ~0xff)
42 + int val = hex2chr(hex);
43 + if (val < 0)
44 return -1;
45 *sha1++ = val;
46 hex += 2;
pkt-line.c
+2 -21
@@ -172,27 +172,8 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size,
172
173 static int packet_length(const char *linelen)
174 {
175 - int n;
176 - int len = 0;
177 -
178 - for (n = 0; n < 4; n++) {
179 - unsigned char c = linelen[n];
180 - len <<= 4;
181 - if (c >= '0' && c <= '9') {
182 - len += c - '0';
183 - continue;
184 - }
185 - if (c >= 'a' && c <= 'f') {
186 - len += c - 'a' + 10;
187 - continue;
188 - }
189 - if (c >= 'A' && c <= 'F') {
190 - len += c - 'A' + 10;
191 - continue;
192 - }
193 - return -1;
194 - }
195 - return len;
175 + int val = hex2chr(linelen);
176 + return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
177 }
178
179 int packet_read(int fd, char **src_buf, size_t *src_len,
pretty.c
+5 -8
@@ -1063,7 +1063,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1063 const struct commit *commit = c->commit;
1064 const char *msg = c->message;
1065 struct commit_list *p;
1066 - int h1, h2;
1066 + int ch;
1067
1068 /* these are independent of the commit */
1069 switch (placeholder[0]) {
@@ -1087,14 +1087,11 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1087 return 1;
1088 case 'x':
1089 /* %x00 == NUL, %x0a == LF, etc. */
1090 - if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
1091 - h1 <= 16 &&
1092 - 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
1093 - h2 <= 16) {
1094 - strbuf_addch(sb, (h1<<4)|h2);
1095 - return 3;
1096 - } else
1090 + ch = hex2chr(placeholder + 1);
1091 + if (ch < 0)
1092 return 0;
1093 + strbuf_addch(sb, ch);
1094 + return 3;
1095 case 'w':
1096 if (placeholder[1] == '(') {
1097 unsigned long width = 0, indent1 = 0, indent2 = 0;
ref-filter.c
+1 -19
@@ -1576,24 +1576,6 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
1576 qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
1577 }
1578
1579 -static int hex1(char ch)
1580 -{
1581 - if ('0' <= ch && ch <= '9')
1582 - return ch - '0';
1583 - else if ('a' <= ch && ch <= 'f')
1584 - return ch - 'a' + 10;
1585 - else if ('A' <= ch && ch <= 'F')
1586 - return ch - 'A' + 10;
1587 - return -1;
1588 -}
1589 -static int hex2(const char *cp)
1590 -{
1591 - if (cp[0] && cp[1])
1592 - return (hex1(cp[0]) << 4) | hex1(cp[1]);
1593 - else
1594 - return -1;
1595 -}
1596 -
1579 static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
1580 {
1581 struct strbuf *s = &state->stack->output;
@@ -1603,7 +1585,7 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting
1585 if (cp[1] == '%')
1586 cp++;
1587 else {
1606 - int ch = hex2(cp + 1);
1588 + int ch = hex2chr(cp + 1);
1589 if (0 <= ch) {
1590 strbuf_addch(s, ch);
1591 cp += 3;
url.c
+1 -20
@@ -29,25 +29,6 @@ int is_url(const char *url)
29 return (url[0] == ':' && url[1] == '/' && url[2] == '/');
30 }
31
32 -static int url_decode_char(const char *q)
33 -{
34 - int i;
35 - unsigned char val = 0;
36 - for (i = 0; i < 2; i++) {
37 - unsigned char c = *q++;
38 - val <<= 4;
39 - if (c >= '0' && c <= '9')
40 - val += c - '0';
41 - else if (c >= 'a' && c <= 'f')
42 - val += c - 'a' + 10;
43 - else if (c >= 'A' && c <= 'F')
44 - val += c - 'A' + 10;
45 - else
46 - return -1;
47 - }
48 - return val;
49 -}
50 -
32 static char *url_decode_internal(const char **query, int len,
33 const char *stop_at, struct strbuf *out,
34 int decode_plus)
@@ -66,7 +47,7 @@ static char *url_decode_internal(const char **query, int len,
47 }
48
49 if (c == '%') {
69 - int val = url_decode_char(q + 1);
50 + int val = hex2chr(q + 1);
51 if (0 <= val) {
52 strbuf_addch(out, val);
53 q += 3;