From 946531bd7b3f6f53fecca8cdabe71218f9cf12a8 Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Sun, 16 Feb 2014 22:09:09 +0200 Subject: [PATCH 1/3] Merge pull request #162 from nmlgc/master Three fixes for hashtable seeding on Windows --- src/hashtable_seed.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/hashtable_seed.c b/src/hashtable_seed.c index cb5f310..7ee8001 100644 --- a/src/hashtable_seed.c +++ b/src/hashtable_seed.c @@ -38,8 +38,8 @@ #endif #if defined(_WIN32) -/* For _getpid() */ -#include +/* For GetModuleHandle(), GetProcAddress() and GetCurrentProcessId() */ +#include #endif #include "jansson.h" @@ -95,7 +95,6 @@ static int seed_from_urandom(uint32_t *seed) { /* Windows Crypto API */ #if defined(_WIN32) && defined(USE_WINDOWS_CRYPTOAPI) -#include #include typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv, LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags); @@ -112,7 +111,7 @@ static int seed_from_windows_cryptoapi(uint32_t *seed) BYTE data[sizeof(uint32_t)]; int ok; - hAdvAPI32 = GetModuleHandle("advapi32.dll"); + hAdvAPI32 = GetModuleHandle(TEXT("advapi32.dll")); if(hAdvAPI32 == NULL) return 1; @@ -131,7 +130,7 @@ static int seed_from_windows_cryptoapi(uint32_t *seed) if (!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) return 1; - ok = CryptGenRandom(hCryptProv, sizeof(uint32_t), data); + ok = pCryptGenRandom(hCryptProv, sizeof(uint32_t), data); pCryptReleaseContext(hCryptProv, 0); if (!ok) @@ -156,7 +155,7 @@ static int seed_from_timestamp_and_pid(uint32_t *seed) { /* XOR with PID for more randomness */ #if defined(_WIN32) - *seed ^= (uint32_t)_getpid(); + *seed ^= (uint32_t)GetCurrentProcessId(); #elif defined(HAVE_GETPID) *seed ^= (uint32_t)getpid(); #endif From 78da1de021bbc1c88577e2cb7ef5b352c620ac25 Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Sun, 16 Feb 2014 22:09:41 +0200 Subject: [PATCH 2/3] Merge pull request #163 from vincentbernat/fix/unpack-mix-optional-and-strict Fix unpack when mixing strict mode and optional keys --- src/pack_unpack.c | 27 ++++++++++++++++++++++----- test/suites/api/test_unpack.c | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/pack_unpack.c b/src/pack_unpack.c index 0d932f7..842fc98 100644 --- a/src/pack_unpack.c +++ b/src/pack_unpack.c @@ -340,6 +340,7 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap) { int ret = -1; int strict = 0; + int gotopt = 0; /* Use a set (emulated by a hashtable) to check that all object keys are accessed. Checking that the correct number of keys @@ -396,7 +397,7 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap) next_token(s); if(token(s) == '?') { - opt = 1; + opt = gotopt = 1; next_token(s); } @@ -422,10 +423,26 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap) if(strict == 0 && (s->flags & JSON_STRICT)) strict = 1; - if(root && strict == 1 && key_set.size != json_object_size(root)) { - long diff = (long)json_object_size(root) - (long)key_set.size; - set_error(s, "", "%li object item(s) left unpacked", diff); - goto out; + if(root && strict == 1) { + /* We need to check that all non optional items have been parsed */ + const char *key; + json_t *value; + long unpacked = 0; + if (gotopt) { + /* We have optional keys, we need to iter on each key */ + json_object_foreach(root, key, value) { + if(!hashtable_get(&key_set, key)) { + unpacked++; + } + } + } else { + /* No optional keys, we can just compare the number of items */ + unpacked = (long)json_object_size(root) - (long)key_set.size; + } + if (unpacked) { + set_error(s, "", "%li object item(s) left unpacked", unpacked); + goto out; + } } ret = 0; diff --git a/test/suites/api/test_unpack.c b/test/suites/api/test_unpack.c index bec8666..ac52bca 100644 --- a/test/suites/api/test_unpack.c +++ b/test/suites/api/test_unpack.c @@ -370,4 +370,23 @@ static void run_tests() if(i1 != 42) fail("json_unpack failed to unpack"); json_decref(j); + + /* Combine ? and ! */ + j = json_pack("{si}", "foo", 42); + i1 = i2 = 0; + if(json_unpack(j, "{sis?i!}", "foo", &i1, "bar", &i2)) + fail("json_unpack failed for optional values with strict mode"); + if(i1 != 42) + fail("json_unpack failed to unpack"); + if(i2 != 0) + fail("json_unpack failed to unpack"); + json_decref(j); + + /* But don't compensate a missing key with an optional one. */ + j = json_pack("{sisi}", "foo", 42, "baz", 43); + i1 = i2 = i3 = 0; + if(!json_unpack_ex(j, &error, 0, "{sis?i!}", "foo", &i1, "bar", &i2)) + fail("json_unpack failed for optional values with strict mode and compensation"); + check_error("1 object item(s) left unpacked", "", 1, 8, 8); + json_decref(j); } From 960ead07f2d9d7c3b709273eebe2b0556826111a Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Sun, 16 Feb 2014 22:10:16 +0200 Subject: [PATCH 3/3] Merge pull request #164 from vincentbernat/fix/gitignore Add new autotools-generated files to .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 9189a93..b2137d6 100644 --- a/.gitignore +++ b/.gitignore @@ -19,10 +19,14 @@ install-sh libtool ltmain.sh missing +compile +test-driver *.lo *.la stamp-h1 *.pyc *.pc /src/jansson_config.h +/jansson_private_config.h.in +/build *.exe