Skip to content

Commit

Permalink
Fix: Memory Leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxwolf authored Jul 28, 2016
2 parents 8512cf3 + 2eb785a commit f071ee6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/jws.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ static bool _cjose_jws_build_sig_ec(
const cjose_jwk_t *jwk,
cjose_err *err)
{
bool retval = false;

// ensure jwk is EC
if (jwk->kty != CJOSE_JWK_KTY_EC)
{
Expand All @@ -579,7 +581,7 @@ static bool _cjose_jws_build_sig_ec(
if (NULL == ecdsa_sig)
{
CJOSE_ERROR(err, CJOSE_ERR_CRYPTO);
return false;
goto _cjose_jws_build_sig_ec_cleanup;
}

// allocate buffer for signature
Expand All @@ -599,7 +601,7 @@ static bool _cjose_jws_build_sig_ec(
if (NULL == jws->sig)
{
CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
return false;
goto _cjose_jws_build_sig_ec_cleanup;
}

memset(jws->sig, 0, jws->sig_len);
Expand All @@ -613,10 +615,16 @@ static bool _cjose_jws_build_sig_ec(
&jws->sig_b64u, &jws->sig_b64u_len, err))
{
CJOSE_ERROR(err, CJOSE_ERR_CRYPTO);
return false;
goto _cjose_jws_build_sig_ec_cleanup;
}

return true;
retval = true;

_cjose_jws_build_sig_ec_cleanup:
if (ecdsa_sig)
ECDSA_SIG_free(ecdsa_sig);

return retval;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 7 additions & 1 deletion test/check_jwe.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ static void _self_encrypt_self_decrypt(const char *plain1)
JWK_RSA,
plain1);

_self_encrypt_self_decrypt_with_key(
CJOSE_HDR_ALG_RSA1_5,
CJOSE_HDR_ENC_A256GCM,
JWK_RSA,
plain1);

_self_encrypt_self_decrypt_with_key(
CJOSE_HDR_ALG_DIR,
CJOSE_HDR_ENC_A256GCM,
Expand Down Expand Up @@ -458,7 +464,7 @@ START_TEST(test_cjose_jwe_import_export_compare)
// re-export the jwe object
const char *cser = cjose_jwe_export(jwe, &err);
ck_assert_msg(NULL != cser,
"re-export of imported JWE faied: "
"re-export of imported JWE failed: "
"%s, file: %s, function: %s, line: %ld",
err.message, err.file, err.function, err.line);

Expand Down
9 changes: 6 additions & 3 deletions test/check_jwk.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ START_TEST(test_cjose_jwk_to_json_ec)
",\"y\":\"KbkZ7r_DQ-t67pnxPnFDHObTLBqn44BSjcqn0STUkaM\"}",
json
);
free(json);

json = cjose_jwk_to_json(jwk, true, &err);
ck_assert(NULL != json);
Expand All @@ -505,6 +506,7 @@ START_TEST(test_cjose_jwk_to_json_ec)
",\"y\":\"KbkZ7r_DQ-t67pnxPnFDHObTLBqn44BSjcqn0STUkaM\""
",\"d\":\"RSSjcBQW_EBxm1gzYhejCdWtj3Id_GuwldwEgSuKCEM\"}",
json);
free(json);

cjose_jwk_release(jwk);
}
Expand Down Expand Up @@ -543,8 +545,7 @@ START_TEST(test_cjose_jwk_to_json_rsa)
const char *json;
json = cjose_jwk_to_json(jwk, false, &err);
ck_assert(NULL != json);
ck_assert_str_eq(RSA_PUBLIC_JSON, json
);
ck_assert_str_eq(RSA_PUBLIC_JSON, json);
free(json);

json = cjose_jwk_to_json(jwk, true, &err);
Expand Down Expand Up @@ -679,7 +680,7 @@ START_TEST(test_cjose_jwk_import_valid)
// get json representation of "after"
const char *jwk_str = cjose_jwk_to_json(jwk, true, &err);
json_t *right_json = json_loads(jwk_str, 0, NULL);
ck_assert(NULL != right_json);
ck_assert(NULL != right_json);

// check that cooresponding attributes match up
const char *attrs[] = { "kty", "crv", "x", "y", "d", "kid",
Expand All @@ -689,6 +690,7 @@ START_TEST(test_cjose_jwk_import_valid)
ck_assert_str_eq(JWK[i], jwk_str);
}

free(jwk_str);
json_decref(left_json);
json_decref(right_json);
cjose_jwk_release(jwk);
Expand Down Expand Up @@ -955,6 +957,7 @@ START_TEST(test_cjose_jwk_EC_import_with_priv_export_with_pub)
ck_assert_str_eq(JWK_OUT, jwk_str);
}

free(jwk_str);
json_decref(left_json);
json_decref(right_json);
cjose_jwk_release(jwk);
Expand Down

0 comments on commit f071ee6

Please sign in to comment.