Skip to content

Commit

Permalink
Fix: remove use of strdup
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxwolf authored Jul 28, 2016
1 parent f071ee6 commit e968f21
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ libcjose_la_SOURCES=version.c \
include/header_int.h \
include/jwk_int.h \
include/jwe_int.h \
include/jws_int.h
include/jws_int.h \
include/util_int.h
3 changes: 2 additions & 1 deletion src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ libcjose_la_SOURCES = version.c \
include/header_int.h \
include/jwk_int.h \
include/jwe_int.h \
include/jws_int.h
include/jws_int.h \
include/util_int.h

all: all-am

Expand Down
17 changes: 17 additions & 0 deletions src/include/util_int.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*!
* Copyrights
*
* Portions created or assigned to Cisco Systems, Inc. are
* Copyright (c) 2014-2016 Cisco Systems, Inc. All Rights Reserved.
*/

#ifndef SRC_UTIL_INT_H
#define SRC_UTIL_INT_H

#include <cjose/error.h>

#include <string.h>

char *_cjose_strndup(const char *str, ssize_t len, cjose_err *err);

#endif // SRC_UTIL_INT_H
18 changes: 8 additions & 10 deletions src/jwe.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "include/header_int.h"
#include "include/jwk_int.h"
#include "include/jwe_int.h"
#include "include/util_int.h"


////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -151,14 +152,16 @@ static bool _cjose_jwe_build_hdr(
}

// copy the serialized header to JWE (hdr_str is owned by header object)
jwe->part[0].raw = (uint8_t *)strdup(hdr_str);
if (NULL == jwe->part[0].raw)
size_t len = strlen(hdr_str);
uint8_t *data = (uint8_t *)_cjose_strndup(hdr_str, len, err);
if (!data)
{
CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
cjose_get_dealloc()(hdr_str);
return false;
}
jwe->part[0].raw_len = strlen(hdr_str);

jwe->part[0].raw = data;
jwe->part[0].raw_len = len;
cjose_get_dealloc()(hdr_str);

return true;
Expand Down Expand Up @@ -1284,12 +1287,7 @@ bool _cjose_jwe_import_part(
}

// copy the b64u part to the jwe
jwe->part[p].b64u = strdup(b64u);
if (NULL == jwe->part[p].b64u)
{
CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
return false;
}
jwe->part[p].b64u = _cjose_strndup(b64u, b64u_len, err);
jwe->part[p].b64u_len = b64u_len;

// b64u decode the part
Expand Down
18 changes: 15 additions & 3 deletions src/jwk.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "include/jwk_int.h"
#include "include/util_int.h"

#include <cjose/base64.h>
#include <cjose/util.h>
Expand Down Expand Up @@ -233,7 +234,12 @@ char *cjose_jwk_to_json(const cjose_jwk_t *jwk, bool priv, cjose_err *err)
CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
goto to_json_cleanup;
}
result = strdup(str_jwk);
result = _cjose_strndup(str_jwk, -1, err);
if (!result)
{
cjose_get_dealloc()(str_jwk);
goto to_json_cleanup;
}
cjose_get_dealloc()(str_jwk);

to_json_cleanup:
Expand Down Expand Up @@ -1599,8 +1605,14 @@ cjose_jwk_t *cjose_jwk_import(const char *jwk_str, size_t len, cjose_err *err)
_get_json_object_string_attribute(jwk_json, CJOSE_JWK_KID_STR, err);
if (kid_str != NULL)
{
jwk->kid = strdup(kid_str);
}
jwk->kid = _cjose_strndup(kid_str, -1, err);
if (!jwk->kid)
{
cjose_jwk_release(jwk);
jwk = NULL;
goto import_cleanup;
}
}

// poor man's "finally"
import_cleanup:
Expand Down
28 changes: 28 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Copyright (c) 2014-2016 Cisco Systems, Inc. All Rights Reserved.
*/

#include "include/util_int.h"

#include <cjose/util.h>

#include <jansson.h>
Expand Down Expand Up @@ -59,5 +61,31 @@ int cjose_const_memcmp(
{
result |= a[i] ^ b[i];
}

return result;
}

char *_cjose_strndup(const char *str, ssize_t len, cjose_err *err)
{
if (NULL == str)
{
CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
return NULL;
}

if (0 > len)
{
len = strlen(str);
}

char *result = cjose_get_alloc()(sizeof(char) * (len + 1));
if (!result)
{
CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
return NULL;
}
memcpy(result, str, len);
result[len] = 0;

return result;
}

0 comments on commit e968f21

Please sign in to comment.