Skip to content

Commit d233097

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e0c1cea commit d233097

File tree

6 files changed

+21
-78
lines changed

6 files changed

+21
-78
lines changed

cache.h

+10
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,16 @@ static inline unsigned int hexval(unsigned char c)
11331133
return hexval_table[c];
11341134
}
11351135

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+
11361146
/* Convert to/from hex/sha1 representation */
11371147
#define MINIMUM_ABBREV minimum_abbrev
11381148
#define DEFAULT_ABBREV default_abbrev

hex.c

+2-10
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,8 @@ int get_sha1_hex(const char *hex, unsigned char *sha1)
3939
{
4040
int i;
4141
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)
5244
return -1;
5345
*sha1++ = val;
5446
hex += 2;

pkt-line.c

+2-21
Original file line numberDiff line numberDiff line change
@@ -172,27 +172,8 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size,
172172

173173
static int packet_length(const char *linelen)
174174
{
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);
196177
}
197178

198179
int packet_read(int fd, char **src_buf, size_t *src_len,

pretty.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
10631063
const struct commit *commit = c->commit;
10641064
const char *msg = c->message;
10651065
struct commit_list *p;
1066-
int h1, h2;
1066+
int ch;
10671067

10681068
/* these are independent of the commit */
10691069
switch (placeholder[0]) {
@@ -1087,14 +1087,11 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
10871087
return 1;
10881088
case 'x':
10891089
/* %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)
10971092
return 0;
1093+
strbuf_addch(sb, ch);
1094+
return 3;
10981095
case 'w':
10991096
if (placeholder[1] == '(') {
11001097
unsigned long width = 0, indent1 = 0, indent2 = 0;

ref-filter.c

+1-19
Original file line numberDiff line numberDiff line change
@@ -1576,24 +1576,6 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
15761576
qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
15771577
}
15781578

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-
15971579
static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
15981580
{
15991581
struct strbuf *s = &state->stack->output;
@@ -1603,7 +1585,7 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting
16031585
if (cp[1] == '%')
16041586
cp++;
16051587
else {
1606-
int ch = hex2(cp + 1);
1588+
int ch = hex2chr(cp + 1);
16071589
if (0 <= ch) {
16081590
strbuf_addch(s, ch);
16091591
cp += 3;

url.c

+1-20
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,6 @@ int is_url(const char *url)
2929
return (url[0] == ':' && url[1] == '/' && url[2] == '/');
3030
}
3131

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-
5132
static char *url_decode_internal(const char **query, int len,
5233
const char *stop_at, struct strbuf *out,
5334
int decode_plus)
@@ -66,7 +47,7 @@ static char *url_decode_internal(const char **query, int len,
6647
}
6748

6849
if (c == '%') {
69-
int val = url_decode_char(q + 1);
50+
int val = hex2chr(q + 1);
7051
if (0 <= val) {
7152
strbuf_addch(out, val);
7253
q += 3;

0 commit comments

Comments
 (0)