Skip to content

Commit fac0836

Browse files
committed
made de85 table initialization explicit and safer
1 parent 2cc7191 commit fac0836

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

base85.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "git-compat-util.h"
22
#include "base85.h"
3+
#include <string.h>
34

45
#undef DEBUG_85
56

@@ -26,15 +27,18 @@ static const char en85[] = {
2627
'|', '}', '~'
2728
};
2829

29-
static char de85[256];
30+
static unsigned char de85[256];
31+
static bool de85_ready;
3032
static void prep_base85(void)
3133
{
32-
if (de85['Z'])
34+
if (de85_ready)
3335
return;
36+
memset(de85, 0, sizeof(de85));
3437
for (size_t i = 0; i < ARRAY_SIZE(en85); i++) {
35-
int ch = en85[i];
36-
de85[ch] = i + 1;
38+
unsigned char ch = (unsigned char)en85[i];
39+
de85[ch] = (unsigned char)(i + 1);
3740
}
41+
de85_ready = true;
3842
}
3943

4044
int decode_85(char *dst, const char *buffer, int len)
@@ -48,13 +52,13 @@ int decode_85(char *dst, const char *buffer, int len)
4852
unsigned char ch;
4953
do {
5054
ch = *buffer++;
51-
de = de85[ch];
55+
de = de85[(unsigned char)ch];
5256
if (--de < 0)
5357
return error("invalid base85 alphabet %c", ch);
5458
acc = acc * 85 + de;
5559
} while (--cnt);
5660
ch = *buffer++;
57-
de = de85[ch];
61+
de = de85[(unsigned char)ch];
5862
if (--de < 0)
5963
return error("invalid base85 alphabet %c", ch);
6064
/* Detect overflow. */

0 commit comments

Comments
 (0)