Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

tighten up tns_strtosz

  • Loading branch information...
commit 91c40a6596d40513d5d0c88b7a76c810c70fa1e8 1 parent 943557e
@rfk authored
Showing with 31 additions and 11 deletions.
  1. +31 −11 tnetstring/tns_core.c
View
42 tnetstring/tns_core.c
@@ -277,28 +277,48 @@ tns_strtosz(const char *data, size_t len, size_t *sz, char **end)
eod = data + len;
// The first character must be a digit.
- c = *pos;
- if(c < '0' || c > '9') {
- return -1;
- }
- value = c - '0';
- pos++;
-
// The netstring spec explicitly forbits padding zeros.
// If it's a zero, it must be the only char in the string.
- if(value == 0) {
- *sz = value;
+ c = *pos++;
+ switch(c) {
+ case '0':
+ *sz = 0;
*end = (char*) pos;
return 0;
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ value = c - '0';
+ break;
+ default:
+ return -1;
}
// Consume all other digits.
while(pos < eod) {
c = *pos;
- if(c < '0' || c > '9') {
+ switch(c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ break;
+ default:
*sz = value;
*end = (char*) pos;
- return 0;
+ return 0;
}
value = (value * 10) + (c - '0');
pos++;
Please sign in to comment.
Something went wrong with that request. Please try again.