From 4d36b0287d3ab0912ba8a4790340ca099960b2b0 Mon Sep 17 00:00:00 2001 From: Tudor Brindus Date: Fri, 1 May 2020 21:09:22 -0400 Subject: [PATCH 01/13] Detect broken RDRAND during initialization Some CPUs advertise RDRAND in CPUID, but return 0xFFFFFFFF unconditionally. To avoid locking up later, test RDRAND during initialization, and if it returns 0xFFFFFFFF, mark it as nonexistent. Fixes #588. --- random_seed.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/random_seed.c b/random_seed.c index c459f0f..4ddcb07 100644 --- a/random_seed.c +++ b/random_seed.c @@ -43,12 +43,41 @@ static void do_cpuid(int regs[], int h) #if HAS_X86_CPUID +static int get_rdrand_seed(void); + +// Valid values are -1 (haven't tested), 0 (no), and 1 (yes). +static int _has_rdrand = -1; + static int has_rdrand(void) { - // CPUID.01H:ECX.RDRAND[bit 30] == 1 - int regs[4]; - do_cpuid(regs, 1); - return (regs[2] & (1 << 30)) != 0; + if (_has_rdrand == -1) + { + // CPUID.01H:ECX.RDRAND[bit 30] == 1 + int regs[4]; + do_cpuid(regs, 1); + if (!(regs[2] & (1 << 30))) + { + _has_rdrand = 0; + } else + { + // Some CPUs advertise RDRAND in CPUID, but return 0xFFFFFFFF + // unconditionally. To avoid locking up later, test RDRAND here. If over + // 10 trials RDRAND has returned the same value, declare it broken. + _has_rdrand = 0; + int prev = get_rdrand_seed(); + for (int i = 0; i < 10; i++) { + int temp = get_rdrand_seed(); + if (temp != prev) { + _has_rdrand = 1; + break; + } + + prev = temp; + } + } + } + + return _has_rdrand; } #endif From 80863140263be5f2dc630938ed8f0066f8a1ab43 Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Mon, 4 May 2020 01:29:02 +0000 Subject: [PATCH 02/13] Issue #589: drop the rdrand test loops to just 3, tweak comments and add some links to bug reports, and decrease the nesting level of the has_rdrand() function. --- random_seed.c | 64 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/random_seed.c b/random_seed.c index 4ddcb07..b5f8a07 100644 --- a/random_seed.c +++ b/random_seed.c @@ -45,36 +45,46 @@ static void do_cpuid(int regs[], int h) static int get_rdrand_seed(void); -// Valid values are -1 (haven't tested), 0 (no), and 1 (yes). +/* Valid values are -1 (haven't tested), 0 (no), and 1 (yes). */ static int _has_rdrand = -1; static int has_rdrand(void) { - if (_has_rdrand == -1) + if (_has_rdrand != -1) { - // CPUID.01H:ECX.RDRAND[bit 30] == 1 - int regs[4]; - do_cpuid(regs, 1); - if (!(regs[2] & (1 << 30))) - { - _has_rdrand = 0; - } else + return _has_rdrand; + } + + /* CPUID.01H:ECX.RDRAND[bit 30] == 1 */ + int regs[4]; + do_cpuid(regs, 1); + if (!(regs[2] & (1 << 30))) + { + _has_rdrand = 0; + return 0; + } + + /* + * Some CPUs advertise RDRAND in CPUID, but return 0xFFFFFFFF + * unconditionally. To avoid locking up later, test RDRAND here. If over + * 3 trials RDRAND has returned the same value, declare it broken. + * Example CPUs are AMD Ryzen 3000 series + * and much older AMD APUs, such as the E1-1500 + * https://github.com/systemd/systemd/issues/11810 + * https://linuxreviews.org/RDRAND_stops_returning_random_values_on_older_AMD_CPUs_after_suspend + */ + _has_rdrand = 0; + int prev = get_rdrand_seed(); + for (int i = 0; i < 3; i++) + { + int temp = get_rdrand_seed(); + if (temp != prev) { - // Some CPUs advertise RDRAND in CPUID, but return 0xFFFFFFFF - // unconditionally. To avoid locking up later, test RDRAND here. If over - // 10 trials RDRAND has returned the same value, declare it broken. - _has_rdrand = 0; - int prev = get_rdrand_seed(); - for (int i = 0; i < 10; i++) { - int temp = get_rdrand_seed(); - if (temp != prev) { - _has_rdrand = 1; - break; - } - - prev = temp; - } + _has_rdrand = 1; + break; } + + prev = temp; } return _has_rdrand; @@ -92,7 +102,7 @@ static int get_rdrand_seed(void) { DEBUG_SEED("get_rdrand_seed"); int _eax; - // rdrand eax + /* rdrand eax */ /* clang-format off */ __asm__ __volatile__("1: .byte 0x0F\n" " .byte 0xC7\n" @@ -132,7 +142,7 @@ static int get_rdrand_seed(void) DEBUG_SEED("get_rdrand_seed"); int _eax; retry: - // rdrand eax + /* rdrand eax */ __asm _emit 0x0F __asm _emit 0xC7 __asm _emit 0xF0 __asm jnc retry __asm mov _eax, eax @@ -206,6 +216,10 @@ static int get_dev_random_seed(void) /* clang-format off */ #include + +/* Caution: these blank lines must remain so clang-format doesn't reorder + includes to put windows.h after wincrypt.h */ + #include /* clang-format on */ #ifndef __GNUC__ From c66e7377f36ba5a20179a61cd0098b8fbdc27d55 Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Mon, 4 May 2020 03:24:39 +0000 Subject: [PATCH 03/13] In jc-bench.sh, decode the --before and --after args. Use a separate data dir to avoid re-downloading files when the work dir is cleared. --- bench/jc-bench.sh | 74 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/bench/jc-bench.sh b/bench/jc-bench.sh index 6eea46f..6cece37 100755 --- a/bench/jc-bench.sh +++ b/bench/jc-bench.sh @@ -77,29 +77,69 @@ done WORK="${RUNDIR}/work" mkdir -p "${WORK}" -# XAX use a different data dir -if [ ! -r "${WORK}/../canada.json" ] ; then - curl -L -o "${WORK}/../canada.json" 'https://github.com/mloskot/json_benchmark/raw/master/data/canada.json' +DATA="${RUNDIR}/data" +mkdir -p "${DATA}" + +if [ ! -r "${DATA}/canada.json" ] ; then + curl -L -o "${DATA}/canada.json" 'https://github.com/mloskot/json_benchmark/raw/master/data/canada.json' fi -# Identify "after" commit hash -after_src_dir=$TOP -after_commit= -if [ ! -z "$after_arg" ] ; then - # XXX decode this in more detail. - # XXX for now, just assume it's a path - after_src_dir=$after_arg +# Identify "after" commit hash, in order of preference +if [ ! -z "$after_arg" -a -d "$after_arg" ] ; then + # Use provided directory + after_src_dir="$after_arg" after_commit= +else + _commit= + if [ ! -z "$after_arg" ] ; then + # Use provided commit hash + _commit=$(git rev-parse --verify "$after_arg") + fi + if [ ! -z "$_commit" ] ;then + after_src_dir= # i.e. current tree + after_commit="$_commit" + else + # Local changes in current working directory + # ${cur_branch} + after_src_dir=$TOP + after_commit= + fi fi -# Identify "before" commit hash -before_src_dir= -#before_commit=origin/master -before_commit=origin/json-c-0.14 -if [ ! -z "$before_arg" ] ; then - # XXX decode this in more detail +# Identify "before" commit hash, in order of preference +if [ ! -z "$before_arg" -a -d "$before_arg" ] ; then + # Use provided directory before_src_dir="$before_arg" before_commit= +else + _commit= + if [ ! -z "$before_arg" ] ; then + # Use provided commit hash + _commit=$(git rev-parse --verify "$before_arg") + fi + if [ ! -z "$_commit" ] ;then + before_src_dir= # i.e. current tree + before_commit="$_commit" + else + # Use origin/${cur_branch}, if different from ${after_commit} + _cur_branch=$(git rev-parse --abbrev-ref HEAD) + _commit= + if [ ! -z "${_cur_branch}" ] ; then + _commit=$(git rev-parse --verify "origin/${_cur_branch}") + fi + if [ "$_commit" = "${after_commit}" ] ; then + _commit= + fi + fi + + if [ ! -z "$_commit" ] ; then + before_src_dir= # i.e. current tree + before_commit="$_commit" + else + # Use previous release + before_src_dir= # i.e. current tree + before_commit="$(git tag | sort | tail -1)" + fi fi compile_benchmark() @@ -162,7 +202,7 @@ run_benchmark() local inst_dir="${WORK}/$bname/install" local bench_dir="${WORK}/$bname/bench" - local INPUT=${WORK}/../canada.json + local INPUT=${DATA}/canada.json cd "${bench_dir}" mkdir -p results From 8e3d3d55449a358c662e73ce6f539d44c8053e2c Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Mon, 4 May 2020 03:40:40 +0000 Subject: [PATCH 04/13] Make the benchmark work with pre-cmake versions of json-c. Fetch a few more data files. --- bench/jc-bench.sh | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/bench/jc-bench.sh b/bench/jc-bench.sh index 6cece37..2cca1f9 100755 --- a/bench/jc-bench.sh +++ b/bench/jc-bench.sh @@ -80,9 +80,14 @@ mkdir -p "${WORK}" DATA="${RUNDIR}/data" mkdir -p "${DATA}" -if [ ! -r "${DATA}/canada.json" ] ; then - curl -L -o "${DATA}/canada.json" 'https://github.com/mloskot/json_benchmark/raw/master/data/canada.json' -fi +for file in citm_catalog.json twitter.json canada.json ; do + if [ ! -r "${DATA}/${file}" ] ; then + echo "Fetching ${file} from github.com/mloskot/json_benchmark" + URL="https://github.com/mloskot/json_benchmark/raw/master/data/${file}" + curl -s -L -o "${DATA}/${file}" "$URL" + fi +done +echo # Identify "after" commit hash, in order of preference if [ ! -z "$after_arg" -a -d "$after_arg" ] ; then @@ -180,8 +185,16 @@ compile_benchmark() fi # else, use the provided $src_dir - cd "${build_dir}" - cmake -DCMAKE_INSTALL_PREFIX="${inst_dir}" "${src_dir}" + if [ -e "${src_dir}/CMakeLists.txt" ] ; then + cd "${build_dir}" + cmake -DCMAKE_INSTALL_PREFIX="${inst_dir}" "${src_dir}" + else + # Old versions of json-c used automake/autoconf + cd "${src_dir}" + sh autogen.sh # always run it, configure doesn't always work + cd "${build_dir}" + "${src_dir}/configure" --prefix="${inst_dir}" + fi make all install cd "${bench_dir}" From 099016b7e8d70a6d5dd814e788bba08d33d48426 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Mon, 4 May 2020 19:41:16 +0200 Subject: [PATCH 05/13] Protect array_list_del_idx against size_t overflow. If the assignment of stop overflows due to idx and count being larger than SIZE_T_MAX in sum, out of boundary access could happen. It takes invalid usage of this function for this to happen, but I decided to add this check so array_list_del_idx is as safe against bad usage as the other arraylist functions. --- arraylist.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arraylist.c b/arraylist.c index 12ad8af..e5524ac 100644 --- a/arraylist.c +++ b/arraylist.c @@ -136,6 +136,9 @@ int array_list_del_idx(struct array_list *arr, size_t idx, size_t count) { size_t i, stop; + /* Avoid overflow in calculation with large indices. */ + if (idx > SIZE_T_MAX - count) + return -1; stop = idx + count; if (idx >= arr->length || stop > arr->length) return -1; From 77d935b7ae7871a1940cd827e850e6063044ec45 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Mon, 4 May 2020 19:46:45 +0200 Subject: [PATCH 06/13] Prevent division by zero in linkhash. If a linkhash with a size of zero is created, then modulo operations are prone to division by zero operations. Purely protective measure against bad usage. --- linkhash.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/linkhash.c b/linkhash.c index 7ea58c0..f05cc38 100644 --- a/linkhash.c +++ b/linkhash.c @@ -12,6 +12,7 @@ #include "config.h" +#include #include #include #include @@ -499,6 +500,8 @@ struct lh_table *lh_table_new(int size, lh_entry_free_fn *free_fn, lh_hash_fn *h int i; struct lh_table *t; + /* Allocate space for elements to avoid divisions by zero. */ + assert(size > 0); t = (struct lh_table *)calloc(1, sizeof(struct lh_table)); if (!t) return NULL; From 952db0f3978b8c55648bcdee2291af4f54e36521 Mon Sep 17 00:00:00 2001 From: dota17 Date: Wed, 6 May 2020 10:48:53 +0800 Subject: [PATCH 07/13] support to build both static and shared libraries --- CMakeLists.txt | 20 ++++++++++++++++++++ README.md | 10 +++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d51dc4..dc84b52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -400,6 +400,26 @@ target_include_directories(${PROJECT_NAME} $ ) +# Allow to build static and shared libraries at the same time +if (BUILD_STATIC_LIBS) + set(ORIGINAL_STATIC_LIB_NAME ${PROJECT_NAME}-static) + add_library(${ORIGINAL_STATIC_LIB_NAME} STATIC + ${JSON_C_SOURCES} + ${JSON_C_HEADERS} + ) + + # rename the static library + set_target_properties(${ORIGINAL_STATIC_LIB_NAME} PROPERTIES + OUTPUT_NAME ${PROJECT_NAME} + ) + + target_include_directories(${PROJECT_NAME} + PUBLIC + $ + $ + ) +endif () + # Always create new install dirs with 0755 permissions, regardless of umask set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS OWNER_READ diff --git a/README.md b/README.md index 39ea0d6..909fd11 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ Variable | Type | Description CMAKE_INSTALL_PREFIX | String | The install location. CMAKE_BUILD_TYPE | String | Defaults to "debug" BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library instead. +BUILD_STATIC_LIBS | Bool | This build generates a static (lib/a) library. ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed ENABLE_THREADING | Bool | Enable partial threading support DISABLE_WERROR | Bool | Disable use of -Werror @@ -106,7 +107,14 @@ DISABLE_BSYMBOLIC | Bool | Disable use of -Bsymbolic-functions Pass these options as `-D` on CMake's command-line. ```sh -cmake -DBUILD_SHARED_LIBS=OFF ... +# build a static library +cmake -DBUILD_SHARED_LIBS=OFF .. +``` + +Allow to build both static and shared libraries. + +```sh +cmake -DBUILD_STATIC_LIBS=ON .. ``` ### Building with partial threading support From d07b91014986900a3a75f306d302e13e005e9d67 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Mon, 4 May 2020 19:47:25 +0200 Subject: [PATCH 08/13] Fix integer overflows. The data structures linkhash and printbuf are limited to 2 GB in size due to a signed integer being used to track their current size. If too much data is added, then size variable can overflow, which is an undefined behaviour in C programming language. Assuming that a signed int overflow just leads to a negative value, like it happens on many sytems (Linux i686/amd64 with gcc), then printbuf is vulnerable to an out of boundary write on 64 bit systems. --- linkhash.c | 7 +++++-- printbuf.c | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/linkhash.c b/linkhash.c index f05cc38..51e90b1 100644 --- a/linkhash.c +++ b/linkhash.c @@ -580,9 +580,12 @@ int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, con { unsigned long n; - if (t->count >= t->size * LH_LOAD_FACTOR) - if (lh_table_resize(t, t->size * 2) != 0) + if (t->count >= t->size * LH_LOAD_FACTOR) { + /* Avoid signed integer overflow with large tables. */ + int new_size = INT_MAX / 2 < t->size ? t->size * 2 : INT_MAX; + if (t->size == INT_MAX || lh_table_resize(t, new_size) != 0) return -1; + } n = h % t->size; diff --git a/printbuf.c b/printbuf.c index 976c12d..00822fa 100644 --- a/printbuf.c +++ b/printbuf.c @@ -15,6 +15,7 @@ #include "config.h" +#include #include #include #include @@ -65,10 +66,16 @@ static int printbuf_extend(struct printbuf *p, int min_size) if (p->size >= min_size) return 0; - - new_size = p->size * 2; - if (new_size < min_size + 8) + /* Prevent signed integer overflows with large buffers. */ + if (min_size > INT_MAX - 8) + return -1; + if (p->size > INT_MAX / 2) new_size = min_size + 8; + else { + new_size = p->size * 2; + if (new_size < min_size + 8) + new_size = min_size + 8; + } #ifdef PRINTBUF_DEBUG MC_DEBUG("printbuf_memappend: realloc " "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) int printbuf_memappend(struct printbuf *p, const char *buf, int size) { + /* Prevent signed integer overflows with large buffers. */ + if (size > INT_MAX - p->bpos - 1) + return -1; if (p->size <= p->bpos + size + 1) { 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) if (offset == -1) offset = pb->bpos; + /* Prevent signed integer overflows with large buffers. */ + if (len > INT_MAX - offset) + return -1; size_needed = offset + len; if (pb->size < size_needed) { From e97fc20bfd99d341174dc61c30ce7f9540786a6f Mon Sep 17 00:00:00 2001 From: dota17 Date: Thu, 7 May 2020 14:50:43 +0800 Subject: [PATCH 09/13] update --- CMakeLists.txt | 7 +------ README.md | 12 +++--------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dc84b52..dc36d18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,6 +69,7 @@ include(GNUInstallDirs) include(CMakePackageConfigHelpers) option(BUILD_SHARED_LIBS "Default to building shared libraries" ON) +option(BUILD_STATIC_LIBS "Default to building static libraries" ON) # Generate a release merge and test it to verify the correctness of republishing the package. ADD_CUSTOM_TARGET(distcheck @@ -412,12 +413,6 @@ if (BUILD_STATIC_LIBS) set_target_properties(${ORIGINAL_STATIC_LIB_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME} ) - - target_include_directories(${PROJECT_NAME} - PUBLIC - $ - $ - ) endif () # Always create new install dirs with 0755 permissions, regardless of umask diff --git a/README.md b/README.md index 909fd11..f5a7ee3 100644 --- a/README.md +++ b/README.md @@ -97,8 +97,8 @@ Variable | Type | Description ---------------------|--------|-------------- CMAKE_INSTALL_PREFIX | String | The install location. CMAKE_BUILD_TYPE | String | Defaults to "debug" -BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library instead. -BUILD_STATIC_LIBS | Bool | This build generates a static (lib/a) library. +BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library only. +BUILD_STATIC_LIBS | Bool | The default build generates a static (lib/a) library. Set this to OFF to create a shared library only. ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed ENABLE_THREADING | Bool | Enable partial threading support DISABLE_WERROR | Bool | Disable use of -Werror @@ -107,16 +107,10 @@ DISABLE_BSYMBOLIC | Bool | Disable use of -Bsymbolic-functions Pass these options as `-D` on CMake's command-line. ```sh -# build a static library +# build a static library only cmake -DBUILD_SHARED_LIBS=OFF .. ``` -Allow to build both static and shared libraries. - -```sh -cmake -DBUILD_STATIC_LIBS=ON .. -``` - ### Building with partial threading support Although json-c does not support fully multi-threaded access to From 929d74512a58412b06e5e3899016b248acf168f5 Mon Sep 17 00:00:00 2001 From: hofnarr Date: Fri, 8 May 2020 02:16:52 +0300 Subject: [PATCH 10/13] cmake: add list for build targets --- CMakeLists.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dc36d18..e7b4086 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -392,7 +392,7 @@ add_library(${PROJECT_NAME} set_target_properties(${PROJECT_NAME} PROPERTIES VERSION 5.0.0 SOVERSION 5) - +list(APPEND CMAKE_TARGETS ${PROJECT_NAME}) # If json-c is used as subroject it set to target correct interface -I flags and allow # to build external target without extra include_directories(...) target_include_directories(${PROJECT_NAME} @@ -413,6 +413,7 @@ if (BUILD_STATIC_LIBS) set_target_properties(${ORIGINAL_STATIC_LIB_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME} ) + list(APPEND CMAKE_TARGETS ${STATIC_LIB}) endif () # Always create new install dirs with 0755 permissions, regardless of umask @@ -426,7 +427,7 @@ set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS WORLD_EXECUTE ) -install(TARGETS ${PROJECT_NAME} +install(TARGETS ${CMAKE_TARGETS} EXPORT ${PROJECT_NAME}-targets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} From 558ef8609cebbab37830e15d34a3716de8b37c73 Mon Sep 17 00:00:00 2001 From: hofnarr Date: Fri, 8 May 2020 02:19:38 +0300 Subject: [PATCH 11/13] cmake: change variable name --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e7b4086..5becd74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -403,14 +403,14 @@ target_include_directories(${PROJECT_NAME} # Allow to build static and shared libraries at the same time if (BUILD_STATIC_LIBS) - set(ORIGINAL_STATIC_LIB_NAME ${PROJECT_NAME}-static) - add_library(${ORIGINAL_STATIC_LIB_NAME} STATIC + set(STATIC_LIB ${PROJECT_NAME}-static) + add_library(${STATIC_LIB} STATIC ${JSON_C_SOURCES} ${JSON_C_HEADERS} ) # rename the static library - set_target_properties(${ORIGINAL_STATIC_LIB_NAME} PROPERTIES + set_target_properties(${STATIC_LIB} PROPERTIES OUTPUT_NAME ${PROJECT_NAME} ) list(APPEND CMAKE_TARGETS ${STATIC_LIB}) From a100573eecf66daa6b50113c016b8a2563b1504e Mon Sep 17 00:00:00 2001 From: hofnarr Date: Fri, 8 May 2020 02:27:06 +0300 Subject: [PATCH 12/13] cmake-configure: fix enable-static option --- cmake-configure | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake-configure b/cmake-configure index 2fcc39e..c8e44ae 100755 --- a/cmake-configure +++ b/cmake-configure @@ -65,7 +65,7 @@ while [ $# -gt 0 ] ; do FLAGS+=(-DBUILD_SHARED_LIBS=ON) ;; --enable-static) - FLAGS+=(-DBUILD_SHARED_LIBS=OFF) + FLAGS+=(-DBUILD_STATIC_LIBS=ON) ;; --disable-Bsymbolic) FLAGS+=(-DDISABLE_BSYMBOLIC=ON) From 090ae4e4d44876e24ea3e3b34cb4d20a58ce57a6 Mon Sep 17 00:00:00 2001 From: Pierce Lopez Date: Fri, 8 May 2020 18:27:35 -0400 Subject: [PATCH 13/13] json_parse demo: fix and use usage() function --- apps/json_parse.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/apps/json_parse.c b/apps/json_parse.c index c10026a..59bf10a 100644 --- a/apps/json_parse.c +++ b/apps/json_parse.c @@ -31,7 +31,6 @@ static const char *fname = NULL; #define json_tokener_get_parse_end(tok) ((tok)->char_offset) #endif -static void usage(int exitval, const char *errmsg); static void showmem(void); static int parseit(int fd, int (*callback)(struct json_object *)); static int showobj(struct json_object *new_obj); @@ -137,14 +136,15 @@ static int showobj(struct json_object *new_obj) return 0; } -static void usage(int exitval, const char *errmsg) +static void usage(const char *argv0, int exitval, const char *errmsg) { FILE *fp = stdout; if (exitval != 0) fp = stderr; if (errmsg != NULL) fprintf(fp, "ERROR: %s\n\n", errmsg); - fprintf(fp, "Usage: %s [-f] [-n] [-s]\n"); + + fprintf(fp, "Usage: %s [-f] [-n] [-s]\n", argv0); fprintf(fp, " -f - Format the output with JSON_C_TO_STRING_PRETTY\n"); fprintf(fp, " -n - No output\n"); fprintf(fp, " -s - Parse in strict mode, flags:\n"); @@ -167,16 +167,15 @@ int main(int argc, char **argv) case 'f': formatted_output = 1; break; case 'n': show_output = 0; break; case 's': strict_mode = 1; break; - default: /* '?' */ fprintf(stderr, "Usage: %s [-f]\n", argv[0]); exit(EXIT_FAILURE); + default: /* '?' */ usage(argv[0], EXIT_FAILURE, NULL); } } - if (optind >= argc) { - fprintf(stderr, "Expected argument after options\n"); - exit(EXIT_FAILURE); + usage(argv[0], EXIT_FAILURE, "Expected argument after options"); } fname = argv[optind]; + int fd = open(argv[optind], O_RDONLY, 0); showmem(); if (parseit(fd, showobj) != 0)