Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cccbdde

Browse files
committed
Be more careful about signed vs. unsigned char
The buildfarm has reminded me that not all systems consider char to be signed and we need to be explicit. Adjust the various bits of mac8.c for what we intend, mostly using casts to unsigned char as suggested by Tom, and adjust the tests for valid input accordingly. Explicitly make the hexlookup table signed as it's useful to use -1 there to indicate an invalid value.
1 parent 7821f72 commit cccbdde

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/backend/utils/adt/mac8.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
#define lobits(addr) \
3636
((unsigned long)(((addr)->e<<24) | ((addr)->f<<16) | ((addr)->g<<8) | ((addr)->h)))
3737

38-
static unsigned char hex2_to_uchar(const char *str, const char *ptr);
38+
static unsigned char hex2_to_uchar(const unsigned char *str, const unsigned char *ptr);
3939

40-
static const char hexlookup[128] = {
40+
static const signed char hexlookup[128] = {
4141
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4242
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4343
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -58,16 +58,16 @@ static const char hexlookup[128] = {
5858
* the entire string, which is used only for error reporting.
5959
*/
6060
static inline unsigned char
61-
hex2_to_uchar(const char *ptr, const char *str)
61+
hex2_to_uchar(const unsigned char *ptr, const unsigned char *str)
6262
{
6363
unsigned char ret = 0;
64-
char lookup;
64+
signed char lookup;
6565

6666
/* Handle the first character */
67-
if (*ptr < 0)
67+
if (*ptr > 127)
6868
goto invalid_input;
6969

70-
lookup = hexlookup[(unsigned char) *ptr];
70+
lookup = hexlookup[*ptr];
7171
if (lookup < 0)
7272
goto invalid_input;
7373

@@ -76,10 +76,10 @@ hex2_to_uchar(const char *ptr, const char *str)
7676
/* Move to the second character */
7777
ptr++;
7878

79-
if (*ptr < 0)
79+
if (*ptr > 127)
8080
goto invalid_input;
8181

82-
lookup = hexlookup[(unsigned char) *ptr];
82+
lookup = hexlookup[*ptr];
8383
if (lookup < 0)
8484
goto invalid_input;
8585

@@ -103,8 +103,8 @@ hex2_to_uchar(const char *ptr, const char *str)
103103
Datum
104104
macaddr8_in(PG_FUNCTION_ARGS)
105105
{
106-
const char *str = PG_GETARG_CSTRING(0);
107-
const char *ptr = str;
106+
const unsigned char *str = (unsigned char*) PG_GETARG_CSTRING(0);
107+
const unsigned char *ptr = str;
108108
macaddr8 *result;
109109
unsigned char a = 0,
110110
b = 0,
@@ -115,10 +115,10 @@ macaddr8_in(PG_FUNCTION_ARGS)
115115
g = 0,
116116
h = 0;
117117
int count = 0;
118-
char spacer = '\0';
118+
unsigned char spacer = '\0';
119119

120120
/* skip leading spaces */
121-
while (*ptr && isspace((unsigned char) *ptr))
121+
while (*ptr && isspace(*ptr))
122122
ptr++;
123123

124124
/* digits must always come in pairs */
@@ -191,9 +191,9 @@ macaddr8_in(PG_FUNCTION_ARGS)
191191
/* allow trailing whitespace after if we have 6 or 8 bytes */
192192
if (count == 6 || count == 8)
193193
{
194-
if (isspace((unsigned char) *ptr))
194+
if (isspace(*ptr))
195195
{
196-
while (*++ptr && isspace((unsigned char) *ptr));
196+
while (*++ptr && isspace(*ptr));
197197

198198
/* If we found a space and then non-space, it's invalid */
199199
if (*ptr)

0 commit comments

Comments
 (0)