Skip to content

Commit 192a6be

Browse files
torvaldsJunio C Hamano
authored andcommitted
fix signed range problems with hex conversions
Make hexval_table[] "const". Also make sure that the accessor function hexval() does not access the table with out-of-range values by declaring its parameter "unsigned char", instead of "unsigned int". With this, gcc can just generate: movzbl (%rdi), %eax movsbl hexval_table(%rax),%edx movzbl 1(%rdi), %eax movsbl hexval_table(%rax),%eax sall $4, %edx orl %eax, %edx for the code to generate a byte from two hex characters. Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7faf068 commit 192a6be

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

cache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ extern int legacy_loose_object(unsigned char *);
302302
extern int has_pack_file(const unsigned char *sha1);
303303
extern int has_pack_index(const unsigned char *sha1);
304304

305-
extern signed char hexval_table[256];
306-
static inline unsigned int hexval(unsigned int c)
305+
extern const signed char hexval_table[256];
306+
static inline unsigned int hexval(unsigned char c)
307307
{
308308
return hexval_table[c];
309309
}

sha1_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const unsigned char null_sha1[20];
3232

3333
static unsigned int sha1_file_open_flag = O_NOATIME;
3434

35-
signed char hexval_table[256] = {
35+
const signed char hexval_table[256] = {
3636
-1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
3737
-1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
3838
-1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */

0 commit comments

Comments
 (0)