Skip to content

Commit

Permalink
Added error checking to int()
Browse files Browse the repository at this point in the history
  • Loading branch information
willemt committed Mar 3, 2014
1 parent c451662 commit 7f02218
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions bencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,31 @@ static int __carry_length(
}

/**
* TODO: This needs to return an error code
* @param end The point that we read out to
* @return number represented by string */
* @param val Output of number represented by string
* @return 0 if error; otherwise 1 */
static long int __read_string_int(
const char *sp,
const char **end
const char **end,
long int *val
)
{
long int val = 0;
*val = 0;

assert(isdigit(*sp));
if (!isdigit(*sp))
return 0;

/* work out number */
do
{
val *= 10;
val += *sp - '0';
*val *= 10;
*val += *sp - '0';
sp++;
}
while (isdigit(*sp));

*end = sp;
return val;
return 1;
}

int bencode_is_dict(
Expand Down Expand Up @@ -148,8 +150,10 @@ static const char *__iterate_to_next_string_pos(
else if (bencode_is_int(&iter))
{
const char *end;
long int val;

__read_string_int(&iter.str[1], &end);
if (0 == __read_string_int(&iter.str[1], &end, &val))
return NULL;

assert(end[0] == 'e');

Expand Down Expand Up @@ -204,7 +208,8 @@ int bencode_int_value(
{
const char *end;

*val = __read_string_int(&be->str[1], &end);
if (0 == __read_string_int(&be->str[1], &end, val))
return 0;

assert(end[0] == 'e');

Expand Down

0 comments on commit 7f02218

Please sign in to comment.