Skip to content

Commit

Permalink
Guard against potential integer overflow (kgabis#133)
Browse files Browse the repository at this point in the history
* Guard against potential integer overflow

If int res holds the value INT_MAX then adding 1 results in undefined
behavior. To guard against this possibility, cast res to size_t, not
the result of res + 1.

Fixes kgabis#132

* Increments version.

* More consitent parentheses when casting to size_t.
  • Loading branch information
yesmar authored and kgabis committed Dec 3, 2019
1 parent 9d63e76 commit 186680a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(parson C)

include (GNUInstallDirs)

set(PARSON_VERSION 1.0.1)
set(PARSON_VERSION 1.0.2)
add_library(parson parson.c)
target_include_directories(parson PUBLIC $<INSTALL_INTERFACE:include>)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parson",
"version": "1.0.1",
"version": "1.0.2",
"repo": "kgabis/parson",
"description": "Small json parser and reader",
"keywords": [ "json", "parser" ],
Expand Down
6 changes: 3 additions & 3 deletions parson.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
SPDX-License-Identifier: MIT
Parson 1.0.1 ( http://kgabis.github.com/parson/ )
Parson 1.0.2 ( http://kgabis.github.com/parson/ )
Copyright (c) 2012 - 2019 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -1496,7 +1496,7 @@ JSON_Value * json_value_deep_copy(const JSON_Value *value) {
size_t json_serialization_size(const JSON_Value *value) {
char num_buf[NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
int res = json_serialize_to_buffer_r(value, NULL, 0, 0, num_buf);
return res < 0 ? 0 : (size_t)(res + 1);
return res < 0 ? 0 : (size_t)(res) + 1;
}

JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) {
Expand Down Expand Up @@ -1556,7 +1556,7 @@ char * json_serialize_to_string(const JSON_Value *value) {
size_t json_serialization_size_pretty(const JSON_Value *value) {
char num_buf[NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
int res = json_serialize_to_buffer_r(value, NULL, 0, 1, num_buf);
return res < 0 ? 0 : (size_t)(res + 1);
return res < 0 ? 0 : (size_t)(res) + 1;
}

JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) {
Expand Down
2 changes: 1 addition & 1 deletion parson.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
SPDX-License-Identifier: MIT
Parson 1.0.1 ( http://kgabis.github.com/parson/ )
Parson 1.0.2 ( http://kgabis.github.com/parson/ )
Copyright (c) 2012 - 2019 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down

0 comments on commit 186680a

Please sign in to comment.