Skip to content

Commit

Permalink
Fix: remove use of strdup in json_stringn
Browse files Browse the repository at this point in the history
use _cjose_strndup in _cjose_json_stringn
  • Loading branch information
linuxwolf authored Jul 28, 2016
2 parents 46a01e1 + aa2ec93 commit ee7ec81
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion doc/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/include/jwk_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/include/util_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

#include <cjose/error.h>

#include <jansson.h>
#include <string.h>

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
14 changes: 5 additions & 9 deletions src/jwk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 21 additions & 6 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit ee7ec81

Please sign in to comment.