From aa2ec936387b110771cfb6c840fefbc49a408e2c Mon Sep 17 00:00:00 2001 From: Hans Zandbelt Date: Thu, 28 Jul 2016 20:09:10 +0200 Subject: [PATCH] use _cjose_strndup in _cjose_json_stringn --- doc/Makefile.in | 2 +- src/include/jwk_int.h | 2 -- src/include/util_int.h | 2 ++ src/jwk.c | 14 +++++--------- src/util.c | 27 +++++++++++++++++++++------ 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/doc/Makefile.in b/doc/Makefile.in index 1e365f7..f1efd0c 100644 --- a/doc/Makefile.in +++ b/doc/Makefile.in @@ -442,8 +442,8 @@ distclean-generic: maintainer-clean-generic: @echo "This command is intended for maintainers to use" @echo "it deletes files that may require special tools to rebuild." -@DX_COND_doc_FALSE@clean-local: @DX_COND_doc_FALSE@install-data-local: +@DX_COND_doc_FALSE@clean-local: clean: clean-am clean-am: clean-generic clean-libtool clean-local mostlyclean-am diff --git a/src/include/jwk_int.h b/src/include/jwk_int.h index 6be2b46..ed62200 100644 --- a/src/include/jwk_int.h +++ b/src/include/jwk_int.h @@ -69,6 +69,4 @@ bool cjose_jwk_hkdf( unsigned int okm_len, cjose_err *err); -json_t *_cjose_json_stringn(const char *value, size_t len); - #endif // SRC_JWK_INT_H diff --git a/src/include/util_int.h b/src/include/util_int.h index 8bf9d63..c2815c3 100644 --- a/src/include/util_int.h +++ b/src/include/util_int.h @@ -10,8 +10,10 @@ #include +#include #include char *_cjose_strndup(const char *str, ssize_t len, cjose_err *err); +json_t *_cjose_json_stringn(const char *value, size_t len, cjose_err *err); #endif // SRC_UTIL_INT_H diff --git a/src/jwk.c b/src/jwk.c index aa8dab3..85169c1 100644 --- a/src/jwk.c +++ b/src/jwk.c @@ -323,7 +323,7 @@ static bool _oct_private_fields( return false; } - field = _cjose_json_stringn(k, klen); + field = _cjose_json_stringn(k, klen, err); cjose_get_dealloc()(k); k = NULL; if (!field) @@ -627,10 +627,9 @@ static bool _EC_public_fields( { goto _ec_to_string_cleanup; } - field = _cjose_json_stringn(b64u, len); + field = _cjose_json_stringn(b64u, len, err); if (!field) { - CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto _ec_to_string_cleanup; } json_object_set(json, "x", field); @@ -647,10 +646,9 @@ static bool _EC_public_fields( { goto _ec_to_string_cleanup; } - field = _cjose_json_stringn(b64u, len); + field = _cjose_json_stringn(b64u, len, err); if (!field) { - CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto _ec_to_string_cleanup; } json_object_set(json, "y", field); @@ -721,10 +719,9 @@ static bool _EC_private_fields( { goto _ec_to_string_cleanup; } - field = _cjose_json_stringn(b64u, len); + field = _cjose_json_stringn(b64u, len, err); if (!field) { - CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto _ec_to_string_cleanup; } json_object_set(json, "d", field); @@ -1007,10 +1004,9 @@ static inline bool _RSA_json_field( { goto RSA_json_field_cleanup; } - field = _cjose_json_stringn(b64u, b64ulen); + field = _cjose_json_stringn(b64u, b64ulen, err); if (!field) { - CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto RSA_json_field_cleanup; } json_object_set(json, name, field); diff --git a/src/util.c b/src/util.c index 29a5351..b782a1c 100644 --- a/src/util.c +++ b/src/util.c @@ -91,13 +91,28 @@ char *_cjose_strndup(const char *str, ssize_t len, cjose_err *err) return result; } -json_t *_cjose_json_stringn(const char *value, size_t len) { +json_t *_cjose_json_stringn(const char *value, size_t len, cjose_err *err) { + json_t *result = NULL; #if JANSSON_VERSION_HEX <= 0x020600 - char *s = strndup(value, len); - json_t *r = json_string(s); - free(s); - return r; + char *s = _cjose_strndup(value, len, err); + if (!s) + { + return NULL; + } + result = json_string(s); + if (!result) + { + CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); + return NULL; + } + cjose_get_dealloc()(s); #else - return json_stringn(value, len); + result = json_stringn(value, len); + if (!result) + { + CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); + return NULL; + } #endif + return result; }