Skip to content

Commit

Permalink
Merge pull request #22 from PJK/pk/typeof_segfault
Browse files Browse the repository at this point in the history
Handle incorrect inputs in the example (fixes #15)
  • Loading branch information
PJK authored Nov 6, 2016
2 parents f83d259 + 1eda0a7 commit ee59904
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Next
- only build tests if explicitely enabled (`-DWITH_TESTS=ON`)
- fixed static header declarations (by cedric-d)
- improved documentation (by Michael Richardson)
- improved `examples/readfile.c`

0.4.0 (2015-12-25)
---------------------
Expand Down
32 changes: 32 additions & 0 deletions examples/readfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,38 @@ int main(int argc, char * argv[])
/* Assuming `buffer` contains `length` bytes of input data */
struct cbor_load_result result;
cbor_item_t * item = cbor_load(buffer, length, &result);

if (result.error.code != CBOR_ERR_NONE) {
printf("There was an error while reading the input near byte %zu (read %zu bytes in total): ", result.error.position, result.read);
switch (result.error.code) {
case CBOR_ERR_MALFORMATED: {
printf("Malformed data\n");
break;
}
case CBOR_ERR_MEMERROR: {
printf("Memory error -- perhaps the input is too large?\n");
break;
}
case CBOR_ERR_NODATA: {
printf("The input is empty\n");
break;
}
case CBOR_ERR_NOTENOUGHDATA: {
printf("Data seem to be missing -- is the input complete?\n");
break;
}
case CBOR_ERR_SYNTAXERROR: {
printf("Syntactically malformed data -- see http://tools.ietf.org/html/rfc7049\n");
break;
}
case CBOR_ERR_NONE: {
// GCC's cheap dataflow analysis gag
break;
}
}
exit(1);
}

/* Pretty-print the result */
cbor_describe(item, stdout);
fflush(stdout);
Expand Down

0 comments on commit ee59904

Please sign in to comment.