2021-01-17 Juan Manuel Guerrero * jansson_private_config.h.in: Define macro FALLTHROUGH to work with GCC7's -Werror=implicit-fallthrough=. * src/dump.c (loop_check): Pacify compiler. * src/hashtable.c (hashtable_do_del, hashtable_do_rehash, hashtable_set): Do not shadow DJGPP function index declared by string.h. * src/hashtable_seed.c (seed_from_urandom): Support plain DOS entropy source (aka /dev/urandom$) and/or random data file. * src/jansson.h: Do not shadow DJGPP function index declared by string.h. * src/lookup3.h (hashlittle): Use new FALLTHROUGH macro in place of each comment. * src/pack_unpack.c (unpack): Use new FALLTHROUGH macro in place of each comment. * src/strconv.c (jsonp_strtod): Set errno to ERANGE if value is either HUGE_VAL or -HUGE_VAL. * src/value.c (json_array_get, json_array_set_new, json_array_insert_new, json_array_remove): Do not shadow DJGPP function index declared by string.h. * test/bin/json_process.c [__DJGPP__]: Include io.h and fcntl.h. [O_BINARY]: If not defined define _O_BINARY to O_BINARY, define _fileno to fileno and define _setmode to setmode. (use_env): Use O_BINARY, fileno and setmode. diff -aprNU5 jansson-2.13.1.orig/jansson_private_config.h.in jansson-2.13.1/jansson_private_config.h.in --- jansson-2.13.1.orig/jansson_private_config.h.in 2020-05-07 18:38:00 +0000 +++ jansson-2.13.1/jansson_private_config.h.in 2021-01-17 20:25:40 +0000 @@ -154,5 +154,14 @@ #undef uint32_t /* Define to the type of an unsigned integer type of width exactly 8 bits if such a type exists and the standard includes do not define it. */ #undef uint8_t + +/* Define macro FALLTHROUGH to work with GCC7's -Werror=implicit-fallthrough=. */ +#ifndef FALLTHROUGH +# if __GNUC__ < 7 +# define FALLTHROUGH ((void) 0) +# else +# define FALLTHROUGH __attribute__ ((__fallthrough__)) +# endif +#endif diff -aprNU5 jansson-2.13.1.orig/src/hashtable.c jansson-2.13.1/src/hashtable.c --- jansson-2.13.1.orig/src/hashtable.c 2020-05-05 03:56:00 +0000 +++ jansson-2.13.1/src/hashtable.c 2021-01-17 20:34:58 +0000 @@ -93,14 +93,14 @@ static pair_t *hashtable_find_pair(hasht /* returns 0 on success, -1 if key was not found */ static int hashtable_do_del(hashtable_t *hashtable, const char *key, size_t hash) { pair_t *pair; bucket_t *bucket; - size_t index; + size_t js_index; - index = hash & hashmask(hashtable->order); - bucket = &hashtable->buckets[index]; + js_index = hash & hashmask(hashtable->order); + bucket = &hashtable->buckets[js_index]; pair = hashtable_find_pair(hashtable, bucket, key, hash); if (!pair) return -1; @@ -136,11 +136,11 @@ static void hashtable_do_clear(hashtable } static int hashtable_do_rehash(hashtable_t *hashtable) { list_t *list, *next; pair_t *pair; - size_t i, index, new_size, new_order; + size_t i, js_index, new_size, new_order; struct hashtable_bucket *new_buckets; new_order = hashtable->order + 1; new_size = hashsize(new_order); @@ -160,12 +160,12 @@ static int hashtable_do_rehash(hashtable list_init(&hashtable->list); for (; list != &hashtable->list; list = next) { next = list->next; pair = list_to_pair(list); - index = pair->hash % new_size; - insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list); + js_index = pair->hash % new_size; + insert_to_bucket(hashtable, &hashtable->buckets[js_index], &pair->list); } return 0; } @@ -194,20 +194,20 @@ void hashtable_close(hashtable_t *hashta } int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value) { pair_t *pair; bucket_t *bucket; - size_t hash, index; + size_t hash, js_index; /* rehash if the load ratio exceeds 1 */ if (hashtable->size >= hashsize(hashtable->order)) if (hashtable_do_rehash(hashtable)) return -1; hash = hash_str(key); - index = hash & hashmask(hashtable->order); - bucket = &hashtable->buckets[index]; + js_index = hash & hashmask(hashtable->order); + bucket = &hashtable->buckets[js_index]; pair = hashtable_find_pair(hashtable, bucket, key, hash); if (pair) { json_decref(pair->value); pair->value = value; diff -aprNU5 jansson-2.13.1.orig/src/hashtable_seed.c jansson-2.13.1/src/hashtable_seed.c --- jansson-2.13.1.orig/src/hashtable_seed.c 2020-05-05 03:56:00 +0000 +++ jansson-2.13.1/src/hashtable_seed.c 2021-01-17 20:38:24 +0000 @@ -40,10 +40,16 @@ #if defined(_WIN32) /* For GetModuleHandle(), GetProcAddress() and GetCurrentProcessId() */ #include #endif +#if defined(__DJGPP__) +#define DEVRANDOM "/dev/urandom\x24" +#else +#define DEVRANDOM "/dev/urandom" +#endif + #include "jansson.h" static uint32_t buf_to_uint32(char *data) { size_t i; uint32_t result = 0; @@ -63,20 +69,36 @@ static int seed_from_urandom(uint32_t *s char data[sizeof(uint32_t)]; int ok; #if defined(HAVE_OPEN) && defined(HAVE_CLOSE) && defined(HAVE_READ) int urandom; - urandom = open("/dev/urandom", O_RDONLY); + urandom = open(DEVRANDOM, O_RDONLY); +#if defined(__DJGPP__) + if (urandom == -1) { + const char *rand_file = getenv("/dev/env/RANFILE"); + if (!rand_file || rand_file[0] == 0) + return 1; + urandom = open(rand_file, O_RDONLY | O_BINARY); + } +#endif /* __DJGPP__ */ if (urandom == -1) return 1; ok = read(urandom, data, sizeof(uint32_t)) == sizeof(uint32_t); close(urandom); #else FILE *urandom; - urandom = fopen("/dev/urandom", "rb"); + urandom = fopen(DEVRANDOM, "rb"); +#if defined(__DJGPP__) + if (!urandom) { + const char *rand_file = getenv("/dev/env/RANFILE"); + if (!rand_file || rand_file[0] == 0) + return 1; + urandom = fopen(rand_file, "rb"); + } +#endif /* __DJGPP__ */ if (!urandom) return 1; ok = fread(data, 1, sizeof(uint32_t), urandom) == sizeof(uint32_t); fclose(urandom); diff -aprNU5 jansson-2.13.1.orig/src/jansson.h jansson-2.13.1/src/jansson.h --- jansson-2.13.1.orig/src/jansson.h 2020-05-07 18:34:30 +0000 +++ jansson-2.13.1/src/jansson.h 2021-01-17 20:41:26 +0000 @@ -252,16 +252,16 @@ static JSON_INLINE int json_object_updat json_decref(other); return ret; } size_t json_array_size(const json_t *array); -json_t *json_array_get(const json_t *array, size_t index) +json_t *json_array_get(const json_t *array, size_t js_index) JANSSON_ATTRS((warn_unused_result)); -int json_array_set_new(json_t *array, size_t index, json_t *value); +int json_array_set_new(json_t *array, size_t js_index, json_t *value); int json_array_append_new(json_t *array, json_t *value); -int json_array_insert_new(json_t *array, size_t index, json_t *value); -int json_array_remove(json_t *array, size_t index); +int json_array_insert_new(json_t *array, size_t js_index, json_t *value); +int json_array_remove(json_t *array, size_t js_index); int json_array_clear(json_t *array); int json_array_extend(json_t *array, json_t *other); static JSON_INLINE int json_array_set(json_t *array, size_t ind, json_t *value) { return json_array_set_new(array, ind, json_incref(value)); diff -aprNU5 jansson-2.13.1.orig/src/lookup3.h jansson-2.13.1/src/lookup3.h --- jansson-2.13.1.orig/src/lookup3.h 2020-05-05 03:56:00 +0000 +++ jansson-2.13.1/src/lookup3.h 2021-01-17 20:43:52 +0000 @@ -308,27 +308,27 @@ static uint32_t hashlittle(const void *k { case 12: c+=k[4]+(((uint32_t)k[5])<<16); b+=k[2]+(((uint32_t)k[3])<<16); a+=k[0]+(((uint32_t)k[1])<<16); break; - case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 11: c+=((uint32_t)k8[10])<<16; FALLTHROUGH; case 10: c+=k[4]; b+=k[2]+(((uint32_t)k[3])<<16); a+=k[0]+(((uint32_t)k[1])<<16); break; - case 9 : c+=k8[8]; /* fall through */ + case 9 : c+=k8[8]; FALLTHROUGH; case 8 : b+=k[2]+(((uint32_t)k[3])<<16); a+=k[0]+(((uint32_t)k[1])<<16); break; - case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 7 : b+=((uint32_t)k8[6])<<16; FALLTHROUGH; case 6 : b+=k[2]; a+=k[0]+(((uint32_t)k[1])<<16); break; - case 5 : b+=k8[4]; /* fall through */ + case 5 : b+=k8[4]; FALLTHROUGH; case 4 : a+=k[0]+(((uint32_t)k[1])<<16); break; - case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 3 : a+=((uint32_t)k8[2])<<16; FALLTHROUGH; case 2 : a+=k[0]; break; case 1 : a+=k8[0]; break; case 0 : return c; /* zero length requires no mixing */ @@ -358,21 +358,21 @@ static uint32_t hashlittle(const void *k } /*-------------------------------- last block: affect all 32 bits of (c) */ switch(length) /* all the case statements fall through */ { - case 12: c+=((uint32_t)k[11])<<24; /* fall through */ - case 11: c+=((uint32_t)k[10])<<16; /* fall through */ - case 10: c+=((uint32_t)k[9])<<8; /* fall through */ - case 9 : c+=k[8]; /* fall through */ - case 8 : b+=((uint32_t)k[7])<<24; /* fall through */ - case 7 : b+=((uint32_t)k[6])<<16; /* fall through */ - case 6 : b+=((uint32_t)k[5])<<8; /* fall through */ - case 5 : b+=k[4]; /* fall through */ - case 4 : a+=((uint32_t)k[3])<<24; /* fall through */ - case 3 : a+=((uint32_t)k[2])<<16; /* fall through */ - case 2 : a+=((uint32_t)k[1])<<8; /* fall through */ + case 12: c+=((uint32_t)k[11])<<24; FALLTHROUGH; + case 11: c+=((uint32_t)k[10])<<16; FALLTHROUGH; + case 10: c+=((uint32_t)k[9])<<8; FALLTHROUGH; + case 9 : c+=k[8]; FALLTHROUGH; + case 8 : b+=((uint32_t)k[7])<<24; FALLTHROUGH; + case 7 : b+=((uint32_t)k[6])<<16; FALLTHROUGH; + case 6 : b+=((uint32_t)k[5])<<8; FALLTHROUGH; + case 5 : b+=k[4]; FALLTHROUGH; + case 4 : a+=((uint32_t)k[3])<<24; FALLTHROUGH; + case 3 : a+=((uint32_t)k[2])<<16; FALLTHROUGH; + case 2 : a+=((uint32_t)k[1])<<8; FALLTHROUGH; case 1 : a+=k[0]; break; case 0 : return c; } } diff -aprNU5 jansson-2.13.1.orig/src/pack_unpack.c jansson-2.13.1/src/pack_unpack.c --- jansson-2.13.1.orig/src/pack_unpack.c 2020-05-05 03:56:00 +0000 +++ jansson-2.13.1/src/pack_unpack.c 2021-01-17 20:45:20 +0000 @@ -786,11 +786,11 @@ static int unpack(scanner_t *s, json_t * return 0; case 'O': if (root && !(s->flags & JSON_VALIDATE_ONLY)) json_incref(root); - /* Fall through */ + FALLTHROUGH; case 'o': if (!(s->flags & JSON_VALIDATE_ONLY)) { json_t **target = va_arg(*ap, json_t **); if (root) diff -aprNU5 jansson-2.13.1.orig/src/strconv.c jansson-2.13.1/src/strconv.c --- jansson-2.13.1.orig/src/strconv.c 2020-05-05 03:56:00 +0000 +++ jansson-2.13.1/src/strconv.c 2021-01-17 20:52:30 +0000 @@ -65,10 +65,16 @@ int jsonp_strtod(strbuffer_t *strbuffer, errno = 0; value = strtod(strbuffer->value, &end); assert(end == strbuffer->value + strbuffer->length); +#ifdef __DJGPP__ +/* Current version of DJGPP's strtod does not set errno correctly. */ + if((value == HUGE_VAL || value == -HUGE_VAL) && errno != ERANGE) + errno = ERANGE; +#endif + if ((value == HUGE_VAL || value == -HUGE_VAL) && errno == ERANGE) { /* Overflow */ return -1; } diff -aprNU5 jansson-2.13.1.orig/src/value.c jansson-2.13.1/src/value.c --- jansson-2.13.1.orig/src/value.c 2020-05-05 03:56:00 +0000 +++ jansson-2.13.1/src/value.c 2021-01-17 20:49:04 +0000 @@ -406,23 +406,23 @@ size_t json_array_size(const json_t *jso return 0; return json_to_array(json)->entries; } -json_t *json_array_get(const json_t *json, size_t index) { +json_t *json_array_get(const json_t *json, size_t js_index) { json_array_t *array; if (!json_is_array(json)) return NULL; array = json_to_array(json); - if (index >= array->entries) + if (js_index >= array->entries) return NULL; - return array->table[index]; + return array->table[js_index]; } -int json_array_set_new(json_t *json, size_t index, json_t *value) { +int json_array_set_new(json_t *json, size_t js_index, json_t *value) { json_array_t *array; if (!value) return -1; @@ -430,17 +430,17 @@ int json_array_set_new(json_t *json, siz json_decref(value); return -1; } array = json_to_array(json); - if (index >= array->entries) { + if (js_index >= array->entries) { json_decref(value); return -1; } - json_decref(array->table[index]); - array->table[index] = value; + json_decref(array->table[js_index]); + array->table[js_index] = value; return 0; } static void array_move(json_array_t *array, size_t dest, size_t src, size_t count) { @@ -499,11 +499,11 @@ int json_array_append_new(json_t *json, array->entries++; return 0; } -int json_array_insert_new(json_t *json, size_t index, json_t *value) { +int json_array_insert_new(json_t *json, size_t js_index, json_t *value) { json_array_t *array; json_t **old_table; if (!value) return -1; @@ -512,11 +512,11 @@ int json_array_insert_new(json_t *json, json_decref(value); return -1; } array = json_to_array(json); - if (index > array->entries) { + if (js_index > array->entries) { json_decref(value); return -1; } old_table = json_array_grow(array, 1, 0); @@ -524,37 +524,37 @@ int json_array_insert_new(json_t *json, json_decref(value); return -1; } if (old_table != array->table) { - array_copy(array->table, 0, old_table, 0, index); - array_copy(array->table, index + 1, old_table, index, array->entries - index); + array_copy(array->table, 0, old_table, 0, js_index); + array_copy(array->table, js_index + 1, old_table, js_index, array->entries - js_index); jsonp_free(old_table); } else - array_move(array, index + 1, index, array->entries - index); + array_move(array, js_index + 1, js_index, array->entries - js_index); - array->table[index] = value; + array->table[js_index] = value; array->entries++; return 0; } -int json_array_remove(json_t *json, size_t index) { +int json_array_remove(json_t *json, size_t js_index) { json_array_t *array; if (!json_is_array(json)) return -1; array = json_to_array(json); - if (index >= array->entries) + if (js_index >= array->entries) return -1; - json_decref(array->table[index]); + json_decref(array->table[js_index]); /* If we're removing the last element, nothing has to be moved */ - if (index < array->entries - 1) - array_move(array, index, index + 1, array->entries - index - 1); + if (js_index < array->entries - 1) + array_move(array, js_index, js_index + 1, array->entries - js_index - 1); array->entries--; return 0; } diff -aprNU5 jansson-2.13.1.orig/test/bin/json_process.c jansson-2.13.1/test/bin/json_process.c --- jansson-2.13.1.orig/test/bin/json_process.c 2020-05-05 03:56:00 +0000 +++ jansson-2.13.1/test/bin/json_process.c 2021-01-17 20:52:30 +0000 @@ -24,10 +24,22 @@ #include /* for _setmode() */ static const char dir_sep = '\\'; #else static const char dir_sep = '/'; +# ifdef __DJGPP__ +# include /* for _setmode() */ +# include /* for O_BINARY */ +# endif +#endif + +#ifndef O_BINARY +# ifdef _O_BINARY +# define O_BINARY _O_BINARY +# define fileno _fileno +# define setmode _setmode +# endif #endif struct config { int indent; int compact; @@ -246,15 +258,15 @@ int use_env() { int indent, precision; size_t flags = 0; json_t *json; json_error_t error; -#ifdef _WIN32 +#if (O_BINARY - 0) /* On Windows, set stdout and stderr to binary mode to avoid outputting DOS line terminators */ - _setmode(_fileno(stdout), _O_BINARY); - _setmode(_fileno(stderr), _O_BINARY); + setmode(fileno(stdout), O_BINARY); + setmode(fileno(stderr), O_BINARY); #endif indent = getenv_int("JSON_INDENT"); if (indent < 0 || indent > 31) { fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);