Skip to content

Commit 45b6416

Browse files
committed
Merge branch 'master' of https://github.com/json-c/json-c
2 parents 9b64c3e + abc9a07 commit 45b6416

9 files changed

+173
-38
lines changed

CMakeLists.txt

+18-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ include(GNUInstallDirs)
6969
include(CMakePackageConfigHelpers)
7070

7171
option(BUILD_SHARED_LIBS "Default to building shared libraries" ON)
72+
option(BUILD_STATIC_LIBS "Default to building static libraries" ON)
7273

7374
# Generate a release merge and test it to verify the correctness of republishing the package.
7475
ADD_CUSTOM_TARGET(distcheck
@@ -391,7 +392,7 @@ add_library(${PROJECT_NAME}
391392
set_target_properties(${PROJECT_NAME} PROPERTIES
392393
VERSION 5.0.0
393394
SOVERSION 5)
394-
395+
list(APPEND CMAKE_TARGETS ${PROJECT_NAME})
395396
# If json-c is used as subroject it set to target correct interface -I flags and allow
396397
# to build external target without extra include_directories(...)
397398
target_include_directories(${PROJECT_NAME}
@@ -400,6 +401,21 @@ target_include_directories(${PROJECT_NAME}
400401
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
401402
)
402403

404+
# Allow to build static and shared libraries at the same time
405+
if (BUILD_STATIC_LIBS)
406+
set(STATIC_LIB ${PROJECT_NAME}-static)
407+
add_library(${STATIC_LIB} STATIC
408+
${JSON_C_SOURCES}
409+
${JSON_C_HEADERS}
410+
)
411+
412+
# rename the static library
413+
set_target_properties(${STATIC_LIB} PROPERTIES
414+
OUTPUT_NAME ${PROJECT_NAME}
415+
)
416+
list(APPEND CMAKE_TARGETS ${STATIC_LIB})
417+
endif ()
418+
403419
# Always create new install dirs with 0755 permissions, regardless of umask
404420
set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
405421
OWNER_READ
@@ -411,7 +427,7 @@ set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
411427
WORLD_EXECUTE
412428
)
413429

414-
install(TARGETS ${PROJECT_NAME}
430+
install(TARGETS ${CMAKE_TARGETS}
415431
EXPORT ${PROJECT_NAME}-targets
416432
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
417433
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ Variable | Type | Description
9797
---------------------|--------|--------------
9898
CMAKE_INSTALL_PREFIX | String | The install location.
9999
CMAKE_BUILD_TYPE | String | Defaults to "debug"
100-
BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library instead.
100+
BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library only.
101+
BUILD_STATIC_LIBS | Bool | The default build generates a static (lib/a) library. Set this to OFF to create a shared library only.
101102
ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed
102103
ENABLE_THREADING | Bool | Enable partial threading support
103104
DISABLE_WERROR | Bool | Disable use of -Werror
@@ -106,7 +107,8 @@ DISABLE_BSYMBOLIC | Bool | Disable use of -Bsymbolic-functions
106107
Pass these options as `-D` on CMake's command-line.
107108

108109
```sh
109-
cmake -DBUILD_SHARED_LIBS=OFF ...
110+
# build a static library only
111+
cmake -DBUILD_SHARED_LIBS=OFF ..
110112
```
111113

112114
### Building with partial threading support

apps/json_parse.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,15 @@ int main(int argc, char **argv)
168168
case 'n': show_output = 0; break;
169169
case 's': strict_mode = 1; break;
170170
case 'h': usage(argv[0], 0, NULL);
171-
default: /* '?' */ usage(argv[0], 1, "Unknown arguments");
171+
default: /* '?' */ usage(argv[0], EXIT_FAILURE, "Unknown arguments");
172172
}
173173
}
174-
175174
if (optind >= argc)
176175
{
177-
fprintf(stderr, "Expected argument after options\n");
178-
exit(EXIT_FAILURE);
176+
usage(argv[0], EXIT_FAILURE, "Expected argument after options");
179177
}
180178
fname = argv[optind];
179+
181180
int fd = open(argv[optind], O_RDONLY, 0);
182181
showmem();
183182
if (parseit(fd, showobj) != 0)

arraylist.c

+3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ int array_list_del_idx(struct array_list *arr, size_t idx, size_t count)
136136
{
137137
size_t i, stop;
138138

139+
/* Avoid overflow in calculation with large indices. */
140+
if (idx > SIZE_T_MAX - count)
141+
return -1;
139142
stop = idx + count;
140143
if (idx >= arr->length || stop > arr->length)
141144
return -1;

bench/jc-bench.sh

+73-20
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,74 @@ done
7777
WORK="${RUNDIR}/work"
7878
mkdir -p "${WORK}"
7979

80-
# XAX use a different data dir
81-
if [ ! -r "${WORK}/../canada.json" ] ; then
82-
curl -L -o "${WORK}/../canada.json" 'https://github.com/mloskot/json_benchmark/raw/master/data/canada.json'
83-
fi
80+
DATA="${RUNDIR}/data"
81+
mkdir -p "${DATA}"
82+
83+
for file in citm_catalog.json twitter.json canada.json ; do
84+
if [ ! -r "${DATA}/${file}" ] ; then
85+
echo "Fetching ${file} from github.com/mloskot/json_benchmark"
86+
URL="https://github.com/mloskot/json_benchmark/raw/master/data/${file}"
87+
curl -s -L -o "${DATA}/${file}" "$URL"
88+
fi
89+
done
90+
echo
8491

85-
# Identify "after" commit hash
86-
after_src_dir=$TOP
87-
after_commit=
88-
if [ ! -z "$after_arg" ] ; then
89-
# XXX decode this in more detail.
90-
# XXX for now, just assume it's a path
91-
after_src_dir=$after_arg
92+
# Identify "after" commit hash, in order of preference
93+
if [ ! -z "$after_arg" -a -d "$after_arg" ] ; then
94+
# Use provided directory
95+
after_src_dir="$after_arg"
9296
after_commit=
97+
else
98+
_commit=
99+
if [ ! -z "$after_arg" ] ; then
100+
# Use provided commit hash
101+
_commit=$(git rev-parse --verify "$after_arg")
102+
fi
103+
if [ ! -z "$_commit" ] ;then
104+
after_src_dir= # i.e. current tree
105+
after_commit="$_commit"
106+
else
107+
# Local changes in current working directory
108+
# ${cur_branch}
109+
after_src_dir=$TOP
110+
after_commit=
111+
fi
93112
fi
94113

95-
# Identify "before" commit hash
96-
before_src_dir=
97-
#before_commit=origin/master
98-
before_commit=origin/json-c-0.14
99-
if [ ! -z "$before_arg" ] ; then
100-
# XXX decode this in more detail
114+
# Identify "before" commit hash, in order of preference
115+
if [ ! -z "$before_arg" -a -d "$before_arg" ] ; then
116+
# Use provided directory
101117
before_src_dir="$before_arg"
102118
before_commit=
119+
else
120+
_commit=
121+
if [ ! -z "$before_arg" ] ; then
122+
# Use provided commit hash
123+
_commit=$(git rev-parse --verify "$before_arg")
124+
fi
125+
if [ ! -z "$_commit" ] ;then
126+
before_src_dir= # i.e. current tree
127+
before_commit="$_commit"
128+
else
129+
# Use origin/${cur_branch}, if different from ${after_commit}
130+
_cur_branch=$(git rev-parse --abbrev-ref HEAD)
131+
_commit=
132+
if [ ! -z "${_cur_branch}" ] ; then
133+
_commit=$(git rev-parse --verify "origin/${_cur_branch}")
134+
fi
135+
if [ "$_commit" = "${after_commit}" ] ; then
136+
_commit=
137+
fi
138+
fi
139+
140+
if [ ! -z "$_commit" ] ; then
141+
before_src_dir= # i.e. current tree
142+
before_commit="$_commit"
143+
else
144+
# Use previous release
145+
before_src_dir= # i.e. current tree
146+
before_commit="$(git tag | sort | tail -1)"
147+
fi
103148
fi
104149

105150
compile_benchmark()
@@ -140,8 +185,16 @@ compile_benchmark()
140185
fi
141186
# else, use the provided $src_dir
142187

143-
cd "${build_dir}"
144-
cmake -DCMAKE_INSTALL_PREFIX="${inst_dir}" "${src_dir}"
188+
if [ -e "${src_dir}/CMakeLists.txt" ] ; then
189+
cd "${build_dir}"
190+
cmake -DCMAKE_INSTALL_PREFIX="${inst_dir}" "${src_dir}"
191+
else
192+
# Old versions of json-c used automake/autoconf
193+
cd "${src_dir}"
194+
sh autogen.sh # always run it, configure doesn't always work
195+
cd "${build_dir}"
196+
"${src_dir}/configure" --prefix="${inst_dir}"
197+
fi
145198
make all install
146199

147200
cd "${bench_dir}"
@@ -162,7 +215,7 @@ run_benchmark()
162215
local inst_dir="${WORK}/$bname/install"
163216
local bench_dir="${WORK}/$bname/bench"
164217

165-
local INPUT=${WORK}/../canada.json
218+
local INPUT=${DATA}/canada.json
166219

167220
cd "${bench_dir}"
168221
mkdir -p results

cmake-configure

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ while [ $# -gt 0 ] ; do
6565
FLAGS+=(-DBUILD_SHARED_LIBS=ON)
6666
;;
6767
--enable-static)
68-
FLAGS+=(-DBUILD_SHARED_LIBS=OFF)
68+
FLAGS+=(-DBUILD_STATIC_LIBS=ON)
6969
;;
7070
--disable-Bsymbolic)
7171
FLAGS+=(-DDISABLE_BSYMBOLIC=ON)

linkhash.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "config.h"
1414

15+
#include <assert.h>
1516
#include <limits.h>
1617
#include <stdarg.h>
1718
#include <stddef.h>
@@ -499,6 +500,8 @@ struct lh_table *lh_table_new(int size, lh_entry_free_fn *free_fn, lh_hash_fn *h
499500
int i;
500501
struct lh_table *t;
501502

503+
/* Allocate space for elements to avoid divisions by zero. */
504+
assert(size > 0);
502505
t = (struct lh_table *)calloc(1, sizeof(struct lh_table));
503506
if (!t)
504507
return NULL;
@@ -577,9 +580,12 @@ int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, con
577580
{
578581
unsigned long n;
579582

580-
if (t->count >= t->size * LH_LOAD_FACTOR)
581-
if (lh_table_resize(t, t->size * 2) != 0)
583+
if (t->count >= t->size * LH_LOAD_FACTOR) {
584+
/* Avoid signed integer overflow with large tables. */
585+
int new_size = INT_MAX / 2 < t->size ? t->size * 2 : INT_MAX;
586+
if (t->size == INT_MAX || lh_table_resize(t, new_size) != 0)
582587
return -1;
588+
}
583589

584590
n = h % t->size;
585591

printbuf.c

+16-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "config.h"
1717

18+
#include <limits.h>
1819
#include <stdio.h>
1920
#include <stdlib.h>
2021
#include <string.h>
@@ -65,10 +66,16 @@ static int printbuf_extend(struct printbuf *p, int min_size)
6566

6667
if (p->size >= min_size)
6768
return 0;
68-
69-
new_size = p->size * 2;
70-
if (new_size < min_size + 8)
69+
/* Prevent signed integer overflows with large buffers. */
70+
if (min_size > INT_MAX - 8)
71+
return -1;
72+
if (p->size > INT_MAX / 2)
7173
new_size = min_size + 8;
74+
else {
75+
new_size = p->size * 2;
76+
if (new_size < min_size + 8)
77+
new_size = min_size + 8;
78+
}
7279
#ifdef PRINTBUF_DEBUG
7380
MC_DEBUG("printbuf_memappend: realloc "
7481
"bpos=%d min_size=%d old_size=%d new_size=%d\n",
@@ -83,6 +90,9 @@ static int printbuf_extend(struct printbuf *p, int min_size)
8390

8491
int printbuf_memappend(struct printbuf *p, const char *buf, int size)
8592
{
93+
/* Prevent signed integer overflows with large buffers. */
94+
if (size > INT_MAX - p->bpos - 1)
95+
return -1;
8696
if (p->size <= p->bpos + size + 1)
8797
{
8898
if (printbuf_extend(p, p->bpos + size + 1) < 0)
@@ -100,6 +110,9 @@ int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
100110

101111
if (offset == -1)
102112
offset = pb->bpos;
113+
/* Prevent signed integer overflows with large buffers. */
114+
if (len > INT_MAX - offset)
115+
return -1;
103116
size_needed = offset + len;
104117
if (pb->size < size_needed)
105118
{

random_seed.c

+47-4
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,51 @@ static void do_cpuid(int regs[], int h)
4343

4444
#if HAS_X86_CPUID
4545

46+
static int get_rdrand_seed(void);
47+
48+
/* Valid values are -1 (haven't tested), 0 (no), and 1 (yes). */
49+
static int _has_rdrand = -1;
50+
4651
static int has_rdrand(void)
4752
{
48-
// CPUID.01H:ECX.RDRAND[bit 30] == 1
53+
if (_has_rdrand != -1)
54+
{
55+
return _has_rdrand;
56+
}
57+
58+
/* CPUID.01H:ECX.RDRAND[bit 30] == 1 */
4959
int regs[4];
5060
do_cpuid(regs, 1);
51-
return (regs[2] & (1 << 30)) != 0;
61+
if (!(regs[2] & (1 << 30)))
62+
{
63+
_has_rdrand = 0;
64+
return 0;
65+
}
66+
67+
/*
68+
* Some CPUs advertise RDRAND in CPUID, but return 0xFFFFFFFF
69+
* unconditionally. To avoid locking up later, test RDRAND here. If over
70+
* 3 trials RDRAND has returned the same value, declare it broken.
71+
* Example CPUs are AMD Ryzen 3000 series
72+
* and much older AMD APUs, such as the E1-1500
73+
* https://github.com/systemd/systemd/issues/11810
74+
* https://linuxreviews.org/RDRAND_stops_returning_random_values_on_older_AMD_CPUs_after_suspend
75+
*/
76+
_has_rdrand = 0;
77+
int prev = get_rdrand_seed();
78+
for (int i = 0; i < 3; i++)
79+
{
80+
int temp = get_rdrand_seed();
81+
if (temp != prev)
82+
{
83+
_has_rdrand = 1;
84+
break;
85+
}
86+
87+
prev = temp;
88+
}
89+
90+
return _has_rdrand;
5291
}
5392

5493
#endif
@@ -63,7 +102,7 @@ static int get_rdrand_seed(void)
63102
{
64103
DEBUG_SEED("get_rdrand_seed");
65104
int _eax;
66-
// rdrand eax
105+
/* rdrand eax */
67106
/* clang-format off */
68107
__asm__ __volatile__("1: .byte 0x0F\n"
69108
" .byte 0xC7\n"
@@ -103,7 +142,7 @@ static int get_rdrand_seed(void)
103142
DEBUG_SEED("get_rdrand_seed");
104143
int _eax;
105144
retry:
106-
// rdrand eax
145+
/* rdrand eax */
107146
__asm _emit 0x0F __asm _emit 0xC7 __asm _emit 0xF0
108147
__asm jnc retry
109148
__asm mov _eax, eax
@@ -177,6 +216,10 @@ static int get_dev_random_seed(void)
177216

178217
/* clang-format off */
179218
#include <windows.h>
219+
220+
/* Caution: these blank lines must remain so clang-format doesn't reorder
221+
includes to put windows.h after wincrypt.h */
222+
180223
#include <wincrypt.h>
181224
/* clang-format on */
182225
#ifndef __GNUC__

0 commit comments

Comments
 (0)