Skip to content

Instantly share code, notes, and snippets.

@bmah888
Created May 11, 2017 17:40
Show Gist options
  • Select an option

  • Save bmah888/f06b3b10f9864d1ee56a77107690785b to your computer and use it in GitHub Desktop.

Select an option

Save bmah888/f06b3b10f9864d1ee56a77107690785b to your computer and use it in GitHub Desktop.
Patch for cJSON to embed it in iperf3 (compatibility and 64-bit fixes)
diff --git a/src/cjson.c b/src/cjson.c
index c50c8ec..a31874d 100644
--- a/src/cjson.c
+++ b/src/cjson.c
@@ -34,13 +34,17 @@
#include <float.h>
#include <limits.h>
#include <ctype.h>
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
+#include <sys/types.h>
#include <locale.h>
#ifdef __GNUC__
#pragma GCC visibility pop
#endif
-#include "cJSON.h"
+#include "cjson.h"
/* define our own boolean type */
#define true ((cJSON_bool)1)
@@ -52,6 +56,13 @@ typedef struct {
} error;
static error global_error = { NULL, 0 };
+#ifndef LLONG_MAX
+#define LLONG_MAX 9223372036854775807LL
+#endif
+#ifndef LLONG_MIN
+#define LLONG_MIN (-LLONG_MAX - 1LL)
+#endif
+
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
{
return (const char*) (global_error.json + global_error.position);
@@ -273,17 +284,17 @@ loop_end:
item->valuedouble = number;
/* use saturation in case of overflow */
- if (number >= INT_MAX)
+ if (number >= LLONG_MAX)
{
- item->valueint = INT_MAX;
+ item->valueint = LLONG_MAX;
}
- else if (number <= INT_MIN)
+ else if (number <= LLONG_MIN)
{
- item->valueint = INT_MIN;
+ item->valueint = LLONG_MIN;
}
else
{
- item->valueint = (int)number;
+ item->valueint = (int64_t)number;
}
item->type = cJSON_Number;
@@ -295,17 +306,17 @@ loop_end:
/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
{
- if (number >= INT_MAX)
+ if (number >= LLONG_MAX)
{
- object->valueint = INT_MAX;
+ object->valueint = LLONG_MAX;
}
- else if (number <= INT_MIN)
+ else if (number <= LLONG_MIN)
{
- object->valueint = INT_MIN;
+ object->valueint = LLONG_MIN;
}
else
{
- object->valueint = (int)number;
+ object->valueint = (int64_t)number;
}
return object->valuedouble = number;
@@ -339,9 +350,9 @@ static unsigned char* ensure(printbuffer * const p, size_t needed)
return NULL;
}
- if (needed > INT_MAX)
+ if (needed > LLONG_MAX)
{
- /* sizes bigger than INT_MAX are currently not supported */
+ /* sizes bigger than LLONG_MAX are currently not supported */
return NULL;
}
@@ -356,12 +367,12 @@ static unsigned char* ensure(printbuffer * const p, size_t needed)
}
/* calculate new buffer size */
- if (needed > (INT_MAX / 2))
+ if (needed > (LLONG_MAX / 2))
{
- /* overflow of int, use INT_MAX if possible */
- if (needed <= INT_MAX)
+ /* overflow of int, use LLONG_MAX if possible */
+ if (needed <= LLONG_MAX)
{
- newsize = INT_MAX;
+ newsize = LLONG_MAX;
}
else
{
@@ -2039,17 +2050,17 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
item->valuedouble = num;
/* use saturation in case of overflow */
- if (num >= INT_MAX)
+ if (num >= LLONG_MAX)
{
- item->valueint = INT_MAX;
+ item->valueint = LLONG_MAX;
}
- else if (num <= INT_MIN)
+ else if (num <= LLONG_MIN)
{
- item->valueint = INT_MIN;
+ item->valueint = LLONG_MIN;
}
else
{
- item->valueint = (int)num;
+ item->valueint = (int64_t)num;
}
}
diff --git a/src/cjson.h b/src/cjson.h
index eccf6bc..fa7cb73 100644
--- a/src/cjson.h
+++ b/src/cjson.h
@@ -19,10 +19,15 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
+#include "iperf_config.h"
#ifndef cJSON__h
#define cJSON__h
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
+
#ifdef __cplusplus
extern "C"
{
@@ -64,7 +69,7 @@ typedef struct cJSON
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
char *valuestring;
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
- int valueint;
+ int64_t valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
@bmah888
Copy link
Author

bmah888 commented May 11, 2017

This patch was used to import cJSON 1.5.2 into iperf (around iperf 3.2), see esnet/iperf#553 and esnet/iperf#573.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment