-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix signed integer oveflow which is UB and silencing other warnings #2
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
test | ||
*.d | ||
base45 | ||
.idea/ | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
|
||
static const char BASE45_CHARSET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; | ||
|
||
static char _C2I[256] = { | ||
static unsigned char _C2I[256] = { | ||
255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255, | ||
255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255, | ||
36, 255,255,255, 37, 38,255,255, 255,255, 39, 40, 255, 41, 42, 43, | ||
|
@@ -47,7 +47,7 @@ int base45_encode(char * dst, size_t *_max_dst_len, const unsigned char * src, s | |
size_t out_len = 0, max_dst_len; | ||
max_dst_len = _max_dst_len ? *_max_dst_len : src_len * 4; | ||
|
||
for(int i = 0; i < src_len; i+=2) { | ||
for(unsigned i = 0; i < src_len; i+=2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. silencing warnings about different sign |
||
if (src_len - i > 1) { | ||
int x = ((src[i])<<8) + src[i+1]; | ||
|
||
|
@@ -88,7 +88,7 @@ int base45_encode(char * dst, size_t *_max_dst_len, const unsigned char * src, s | |
return 0; | ||
} | ||
|
||
int base45_decode(unsigned char * dst, size_t * _max_dst_len, const char * src, size_t src_len) { | ||
int base45_decode(unsigned char * dst, size_t * _max_dst_len, const char *src, size_t src_len) { | ||
size_t out_len = 0, max_dst_len; | ||
max_dst_len = _max_dst_len ? *_max_dst_len : src_len; | ||
|
||
|
@@ -101,19 +101,19 @@ int base45_decode(unsigned char * dst, size_t * _max_dst_len, const char * src, | |
if (src_len == 0) | ||
src_len = strlen(src); | ||
|
||
for(int i = 0; i < src_len; i+=3) { | ||
for(unsigned i = 0; i < src_len; i+=3) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. silencing warnings about different sign |
||
int x,a,b; | ||
|
||
if (src_len - i < 2) | ||
return -1; | ||
return -1; | ||
|
||
if ((255 == (a = _C2I[src[i]])) || (255 == (b = _C2I[src[i+1]]))) | ||
if ((255 == (a = _C2I[(unsigned char)src[i]])) || (255 == (b = _C2I[(unsigned char)src[i+1]]))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unsigned integer overflow is well defined, so this cast silence other useless warnings |
||
return -1; | ||
|
||
x = a + 45 * b; | ||
|
||
if (src_len - i >= 3) { | ||
if (255 == (a = _C2I[src[i+2]])) | ||
if (255 == (a = _C2I[(unsigned char)src[i+2]])) | ||
return -1; | ||
|
||
x += a * 45 * 45; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ void check(char * in, char * out) { | |
assert(0 == bcmp(dec,in,dlen)); | ||
|
||
printf("base64(\"%s\") -> \"%s\"\n", (char*)dec, enc); | ||
}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C standards doesn't allow ; after function definitions |
||
|
||
int main(int argc, char **argv) { | ||
check("Hello!!","%69 VD92EX0"); | ||
|
@@ -30,4 +30,4 @@ int main(int argc, char **argv) { | |
check("COVID-19","-M8*+A%R81A6"); | ||
check("2021 Digital Green Certificates for travel", | ||
"NF6OF6P34SED-EDAECS34ZKE1$CO345$CBWER.CGPC7WE.OEX.CBJEKWEKEC: C"); | ||
}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C standards doesn't allow ; after function definitions |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
test.o: test.c base45.h | ||
|
||
base45.h: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nothing just prevent to push my ide folder