[pkg-opensc-commit] [opensc] 226/295: replace assert with error handling

Eric Dorland eric at moszumanska.debian.org
Sat Jun 24 21:11:34 UTC 2017


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository opensc.

commit e7915ec198a4f2861141157b8fcd6e4e1322eaff
Author: Frank Morgner <frankmorgner at gmail.com>
Date:   Wed Feb 22 09:32:18 2017 +0100

    replace assert with error handling
---
 src/libopensc/card-sc-hsm.c   |  8 +++-
 src/libopensc/card.c          | 90 ++++++++++++++++++++++++++++++++-----------
 src/libopensc/ctx.c           | 12 ++++--
 src/libopensc/iso7816.c       | 24 +++++++++---
 src/libopensc/log.c           |  4 +-
 src/libopensc/pkcs15-cert.c   |  8 +++-
 src/libopensc/pkcs15-pin.c    |  4 +-
 src/libopensc/pkcs15-pubkey.c | 20 +++++++---
 src/libopensc/pkcs15.c        | 22 +++++++----
 src/libopensc/sc.c            | 50 +++++++++++++++---------
 src/libopensc/sec.c           | 20 +++++++---
 11 files changed, 189 insertions(+), 73 deletions(-)

diff --git a/src/libopensc/card-sc-hsm.c b/src/libopensc/card-sc-hsm.c
index adaaff4..d7714e7 100644
--- a/src/libopensc/card-sc-hsm.c
+++ b/src/libopensc/card-sc-hsm.c
@@ -540,7 +540,9 @@ static int sc_hsm_compute_signature(sc_card_t *card,
 	u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
 	sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data;
 
-	assert(card != NULL && data != NULL && out != NULL);
+	if (card == NULL || data == NULL || out == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	if (priv->env == NULL) {
 		LOG_FUNC_RETURN(card->ctx, SC_ERROR_OBJECT_NOT_FOUND);
@@ -590,7 +592,9 @@ static int sc_hsm_decipher(sc_card_t *card, const u8 * crgram, size_t crgram_len
 	u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
 	sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data;
 
-	assert(card != NULL && crgram != NULL && out != NULL);
+	if (card == NULL || crgram == NULL || out == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(card->ctx);
 
 	sc_format_apdu(card, &apdu, SC_APDU_CASE_4, 0x62, priv->env->key_ref[0], priv->algorithm);
diff --git a/src/libopensc/card.c b/src/libopensc/card.c
index fba0832..46091aa 100644
--- a/src/libopensc/card.c
+++ b/src/libopensc/card.c
@@ -56,7 +56,9 @@ int sc_check_sw(sc_card_t *card, unsigned int sw1, unsigned int sw2)
 void sc_format_apdu(sc_card_t *card, sc_apdu_t *apdu,
 		    int cse, int ins, int p1, int p2)
 {
-	assert(card != NULL && apdu != NULL);
+	if (card == NULL || apdu == NULL) {
+		return;
+	}
 	memset(apdu, 0, sizeof(*apdu));
 	apdu->cla = (u8) card->cla;
 	apdu->cse = cse;
@@ -135,7 +137,9 @@ static void sc_card_free(sc_card_t *card)
 size_t sc_get_max_recv_size(const sc_card_t *card)
 {
 	size_t max_recv_size;
-	assert(card != NULL && card->reader != NULL);
+	if (card == NULL || card->reader == NULL) {
+		return 0;
+	}
 	max_recv_size = card->max_recv_size;
 
 	/* initialize max_recv_size to a meaningfull value */
@@ -159,7 +163,9 @@ size_t sc_get_max_send_size(const sc_card_t *card)
 {
 	size_t max_send_size;
 
-	assert(card != NULL && card->reader != NULL);
+	if (card == NULL || card->reader == NULL) {
+		return 0;
+	}
 
 	max_send_size = card->max_send_size;
 
@@ -339,7 +345,8 @@ int sc_disconnect_card(sc_card_t *card)
 	ctx = card->ctx;
 	LOG_FUNC_CALLED(ctx);
 
-	assert(card->lock_count == 0);
+	if (card->lock_count != 0)
+		return SC_ERROR_NOT_ALLOWED;
 	if (card->ops->finish) {
 		int r = card->ops->finish(card);
 		if (r)
@@ -455,7 +462,9 @@ int sc_unlock(sc_card_t *card)
 	if (r != SC_SUCCESS)
 		return r;
 
-	assert(card->lock_count >= 1);
+	if (card->lock_count < 1) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	if (--card->lock_count == 0) {
 #ifdef INVALIDATE_CARD_CACHE_IN_UNLOCK
 		/* invalidate cache */
@@ -480,7 +489,9 @@ int sc_list_files(sc_card_t *card, u8 *buf, size_t buflen)
 {
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(card->ctx);
 
 	if (card->ops->list_files == NULL)
@@ -496,7 +507,9 @@ int sc_create_file(sc_card_t *card, sc_file_t *file)
 	char pbuf[SC_MAX_PATH_STRING_SIZE];
 	const sc_path_t *in_path;
 
-	assert(card != NULL && file != NULL);
+	if (card == NULL || file == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	in_path = &file->path;
 	r = sc_path_print(pbuf, sizeof(pbuf), in_path);
@@ -523,7 +536,9 @@ int sc_delete_file(sc_card_t *card, const sc_path_t *path)
 	int r;
 	char pbuf[SC_MAX_PATH_STRING_SIZE];
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	r = sc_path_print(pbuf, sizeof(pbuf), path);
 	if (r != SC_SUCCESS)
@@ -543,7 +558,9 @@ int sc_read_binary(sc_card_t *card, unsigned int idx,
 	size_t max_le = sc_get_max_recv_size(card);
 	int r;
 
-	assert(card != NULL && card->ops != NULL && buf != NULL);
+	if (card == NULL || card->ops == NULL || buf == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	sc_log(card->ctx, "called; %"SC_FORMAT_LEN_SIZE_T"u bytes at index %d",
 	       count, idx);
 	if (count == 0)
@@ -594,7 +611,9 @@ int sc_write_binary(sc_card_t *card, unsigned int idx,
 	size_t max_lc = sc_get_max_send_size(card);
 	int r;
 
-	assert(card != NULL && card->ops != NULL && buf != NULL);
+	if (card == NULL || card->ops == NULL || buf == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	sc_log(card->ctx, "called; %"SC_FORMAT_LEN_SIZE_T"u bytes at index %d",
 	       count, idx);
 	if (count == 0)
@@ -638,7 +657,9 @@ int sc_update_binary(sc_card_t *card, unsigned int idx,
 	size_t max_lc = sc_get_max_send_size(card);
 	int r;
 
-	assert(card != NULL && card->ops != NULL && buf != NULL);
+	if (card == NULL || card->ops == NULL || buf == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	sc_log(card->ctx, "called; %"SC_FORMAT_LEN_SIZE_T"u bytes at index %d",
 	       count, idx);
 	if (count == 0)
@@ -690,7 +711,9 @@ int sc_erase_binary(struct sc_card *card, unsigned int offs, size_t count,  unsi
 {
 	int r;
 
-	assert(card != NULL && card->ops != NULL);
+	if (card == NULL || card->ops == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	sc_log(card->ctx,
 	       "called; erase %"SC_FORMAT_LEN_SIZE_T"u bytes from offset %d",
 	       count, offs);
@@ -708,7 +731,9 @@ int sc_select_file(sc_card_t *card, const sc_path_t *in_path,  sc_file_t **file)
 	int r;
 	char pbuf[SC_MAX_PATH_STRING_SIZE];
 
-	assert(card != NULL && in_path != NULL);
+	if (card == NULL || in_path == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	r = sc_path_print(pbuf, sizeof(pbuf), in_path);
 	if (r != SC_SUCCESS)
@@ -788,7 +813,9 @@ int sc_get_challenge(sc_card_t *card, u8 *rnd, size_t len)
 {
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(card->ctx);
 
 	if (card->ops->get_challenge == NULL)
@@ -803,7 +830,9 @@ int sc_read_record(sc_card_t *card, unsigned int rec_nr, u8 *buf,
 {
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(card->ctx);
 
 	if (card->ops->read_record == NULL)
@@ -818,7 +847,9 @@ int sc_write_record(sc_card_t *card, unsigned int rec_nr, const u8 * buf,
 {
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(card->ctx);
 
 	if (card->ops->write_record == NULL)
@@ -833,7 +864,9 @@ int sc_append_record(sc_card_t *card, const u8 * buf, size_t count,
 {
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(card->ctx);
 
 	if (card->ops->append_record == NULL)
@@ -848,7 +881,9 @@ int sc_update_record(sc_card_t *card, unsigned int rec_nr, const u8 * buf,
 {
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(card->ctx);
 
 	if (card->ops->update_record == NULL)
@@ -862,7 +897,9 @@ int sc_delete_record(sc_card_t *card, unsigned int rec_nr)
 {
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(card->ctx);
 
 	if (card->ops->delete_record == NULL)
@@ -877,7 +914,9 @@ sc_card_ctl(sc_card_t *card, unsigned long cmd, void *args)
 {
 	int r = SC_ERROR_NOT_SUPPORTED;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(card->ctx);
 
 	if (card->ops->card_ctl != NULL)
@@ -895,7 +934,9 @@ int _sc_card_add_algorithm(sc_card_t *card, const sc_algorithm_info_t *info)
 {
 	sc_algorithm_info_t *p;
 
-	assert(info != NULL);
+	if (info == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	p = (sc_algorithm_info_t *) realloc(card->algorithms, (card->algorithm_count + 1) * sizeof(*info));
 	if (!p) {
 		if (card->algorithms)
@@ -1182,7 +1223,8 @@ scconf_block *sc_get_conf_block(sc_context_t *ctx, const char *name1, const char
 void sc_print_cache(struct sc_card *card)   {
 	struct sc_context *ctx = NULL;
 
-	assert(card != NULL);
+	if (card == NULL)
+		return;
 	ctx = card->ctx;
 
 	if (!card->cache.valid || (!card->cache.current_ef && !card->cache.current_df))   {
@@ -1261,7 +1303,9 @@ sc_card_sm_load(struct sc_card *card, const char *module_path, const char *in_mo
 	const char path_delim = '/';
 #endif
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	ctx = card->ctx;
 	SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_NORMAL);
 	if (!in_module)
diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c
index 81a9bd7..fb42477 100644
--- a/src/libopensc/ctx.c
+++ b/src/libopensc/ctx.c
@@ -41,7 +41,9 @@
 
 int _sc_add_reader(sc_context_t *ctx, sc_reader_t *reader)
 {
-	assert(reader != NULL);
+	if (reader == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	reader->ctx = ctx;
 	list_append(&ctx->readers, reader);
 	return SC_SUCCESS;
@@ -49,7 +51,9 @@ int _sc_add_reader(sc_context_t *ctx, sc_reader_t *reader)
 
 int _sc_delete_reader(sc_context_t *ctx, sc_reader_t *reader)
 {
-	assert(reader != NULL);
+	if (reader == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	if (reader->ops->release)
 			reader->ops->release(reader);
 	if (reader->name)
@@ -867,7 +871,9 @@ int sc_release_context(sc_context_t *ctx)
 {
 	unsigned int i;
 
-	assert(ctx != NULL);
+	if (ctx == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE);
 	while (list_size(&ctx->readers)) {
 		sc_reader_t *rdr = (sc_reader_t *) list_get_at(&ctx->readers, 0);
diff --git a/src/libopensc/iso7816.c b/src/libopensc/iso7816.c
index d956def..3edb582 100644
--- a/src/libopensc/iso7816.c
+++ b/src/libopensc/iso7816.c
@@ -34,7 +34,9 @@
 static void fixup_transceive_length(const struct sc_card *card,
 		struct sc_apdu *apdu)
 {
-	assert(card != NULL && apdu != NULL);
+	if (card == NULL || apdu == NULL) {
+		return;
+	}
 
 	if (apdu->lc > sc_get_max_send_size(card)) {
 		/* The lower layers will automatically do chaining */
@@ -459,7 +461,9 @@ iso7816_select_file(struct sc_card *card, const struct sc_path *in_path, struct
 	size_t buffer_len;
 	unsigned int cla, tag;
 
-	assert(card != NULL && in_path != NULL);
+	if (card == NULL || in_path == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	ctx = card->ctx;
 	memcpy(path, in_path->value, in_path->len);
 	pathlen = in_path->len;
@@ -803,7 +807,9 @@ iso7816_set_security_env(struct sc_card *card,
 	u8 *p;
 	int r, locked = 0;
 
-	assert(card != NULL && env != NULL);
+	if (card == NULL || env == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0x41, 0);
 	switch (env->operation) {
 	case SC_SEC_OPERATION_DECIPHER:
@@ -880,7 +886,9 @@ iso7816_restore_security_env(struct sc_card *card, int se_num)
 	struct sc_apdu apdu;
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x22, 0xF3, se_num);
 
@@ -901,7 +909,9 @@ iso7816_compute_signature(struct sc_card *card,
 	int r;
 	struct sc_apdu apdu;
 
-	assert(card != NULL && data != NULL && out != NULL);
+	if (card == NULL || data == NULL || out == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(card->ctx);
 	sc_log(card->ctx,
 	       "ISO7816 compute signature: in-len %"SC_FORMAT_LEN_SIZE_T"u, out-len %"SC_FORMAT_LEN_SIZE_T"u",
@@ -941,7 +951,9 @@ iso7816_decipher(struct sc_card *card,
 	struct sc_apdu apdu;
 	u8 *sbuf = NULL;
 
-	assert(card != NULL && crgram != NULL && out != NULL);
+	if (card == NULL || crgram == NULL || out == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(card->ctx);
 	sc_log(card->ctx,
 	       "ISO7816 decipher: in-len %"SC_FORMAT_LEN_SIZE_T"u, out-len %"SC_FORMAT_LEN_SIZE_T"u",
diff --git a/src/libopensc/log.c b/src/libopensc/log.c
index 5edb732..f26eadf 100644
--- a/src/libopensc/log.c
+++ b/src/libopensc/log.c
@@ -186,7 +186,9 @@ void sc_hex_dump(struct sc_context *ctx, int level, const u8 * in, size_t count,
 	if (!ctx || ctx->debug < level)
 		return;
 
-	assert(buf != NULL && (in != NULL || count == 0));
+	if (buf == NULL || (in == NULL && count != 0)) {
+		return;
+	}
 	buf[0] = 0;
 	if ((count * 5) > len)
 		return;
diff --git a/src/libopensc/pkcs15-cert.c b/src/libopensc/pkcs15-cert.c
index 440f531..fdc331d 100644
--- a/src/libopensc/pkcs15-cert.c
+++ b/src/libopensc/pkcs15-cert.c
@@ -361,7 +361,9 @@ sc_pkcs15_read_certificate(struct sc_pkcs15_card *p15card, const struct sc_pkcs1
 	struct sc_pkcs15_der der;
 	int r;
 
-	assert(p15card != NULL && info != NULL && cert_out != NULL);
+	if (p15card == NULL || info == NULL || cert_out == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	ctx = p15card->card->ctx;
 	LOG_FUNC_CALLED(ctx);
 
@@ -533,7 +535,9 @@ sc_pkcs15_encode_cdf_entry(sc_context_t *ctx, const struct sc_pkcs15_object *obj
 void
 sc_pkcs15_free_certificate(struct sc_pkcs15_cert *cert)
 {
-	assert(cert != NULL);
+	if (cert == NULL) {
+		return;
+	}
 
 	if (cert->key)
 		sc_pkcs15_free_pubkey(cert->key);
diff --git a/src/libopensc/pkcs15-pin.c b/src/libopensc/pkcs15-pin.c
index 5e1255c..8114d06 100644
--- a/src/libopensc/pkcs15-pin.c
+++ b/src/libopensc/pkcs15-pin.c
@@ -257,7 +257,9 @@ static int
 _validate_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_auth_info *auth_info, size_t pinlen)
 {
 	size_t max_length;
-	assert(p15card != NULL);
+	if (p15card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	/* Ignore validation of the non-PIN authentication objects */
 	if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
diff --git a/src/libopensc/pkcs15-pubkey.c b/src/libopensc/pkcs15-pubkey.c
index 536b8b2..6aab12c 100644
--- a/src/libopensc/pkcs15-pubkey.c
+++ b/src/libopensc/pkcs15-pubkey.c
@@ -910,7 +910,9 @@ sc_pkcs15_read_pubkey(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_obj
 	size_t	len;
 	int	algorithm, r;
 
-	assert(p15card != NULL && obj != NULL && out != NULL);
+	if (p15card == NULL || obj == NULL || out == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	LOG_FUNC_CALLED(ctx);
 	sc_log(ctx, "Public key type 0x%X", obj->type);
 
@@ -997,7 +999,9 @@ err:
 static int
 sc_pkcs15_dup_bignum (struct sc_pkcs15_bignum *dst, struct sc_pkcs15_bignum *src)
 {
-	assert(dst && src);
+	if (!dst || !src) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	if (src->data && src->len)   {
 		dst->data = calloc(1, src->len);
@@ -1018,7 +1022,9 @@ sc_pkcs15_pubkey_from_prvkey(struct sc_context *ctx, struct sc_pkcs15_prkey *prv
 	struct sc_pkcs15_pubkey *pubkey = NULL;
 	int rv = SC_SUCCESS;
 
-	assert(prvkey && out);
+	if (!prvkey || !out) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	*out = NULL;
 	pubkey = calloc(1, sizeof(struct sc_pkcs15_pubkey));
@@ -1074,7 +1080,9 @@ sc_pkcs15_dup_pubkey(struct sc_context *ctx, struct sc_pkcs15_pubkey *key, struc
 	u8* alg;
 	size_t alglen;
 
-	assert(key && out);
+	if (!key || !out) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	*out = NULL;
 	pubkey = calloc(1, sizeof(struct sc_pkcs15_pubkey));
@@ -1149,7 +1157,9 @@ sc_pkcs15_dup_pubkey(struct sc_context *ctx, struct sc_pkcs15_pubkey *key, struc
 void
 sc_pkcs15_erase_pubkey(struct sc_pkcs15_pubkey *key)
 {
-	assert(key != NULL);
+	if (key == NULL) {
+		return;
+	}
 	if (key->alg_id) {
 		sc_asn1_clear_algorithm_id(key->alg_id);
 		free(key->alg_id);
diff --git a/src/libopensc/pkcs15.c b/src/libopensc/pkcs15.c
index 95b38b4..ad2a0ee 100644
--- a/src/libopensc/pkcs15.c
+++ b/src/libopensc/pkcs15.c
@@ -767,9 +767,8 @@ sc_pkcs15_free_app(struct sc_pkcs15_card *p15card)
 void
 sc_pkcs15_card_free(struct sc_pkcs15_card *p15card)
 {
-	if (p15card == NULL)
+	if (p15card == NULL || p15card->magic != SC_PKCS15_CARD_MAGIC)
 		return;
-	assert(p15card->magic == SC_PKCS15_CARD_MAGIC);
 
 	if (p15card->ops.clear)
 		p15card->ops.clear(p15card);
@@ -1189,7 +1188,9 @@ sc_pkcs15_bind(struct sc_card *card, struct sc_aid *aid,
 	LOG_FUNC_CALLED(ctx);
 	sc_log(ctx, "application(aid:'%s')", aid ? sc_dump_hex(aid->value, aid->len) : "empty");
 
-	assert(p15card_out != NULL);
+	if (p15card_out == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	p15card = sc_pkcs15_card_new();
 	if (p15card == NULL)
 		LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
@@ -1261,7 +1262,9 @@ error:
 int
 sc_pkcs15_unbind(struct sc_pkcs15_card *p15card)
 {
-	assert(p15card != NULL && p15card->magic == SC_PKCS15_CARD_MAGIC);
+	if (p15card == NULL || p15card->magic != SC_PKCS15_CARD_MAGIC) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	LOG_FUNC_CALLED(p15card->card->ctx);
 	if (p15card->dll_handle)
@@ -1960,7 +1963,9 @@ sc_pkcs15_encode_df(struct sc_context *ctx, struct sc_pkcs15_card *p15card, stru
 		     unsigned char **nbuf, size_t *nbufsize) = NULL;
 	int r;
 
-	assert(p15card != NULL && p15card->magic == SC_PKCS15_CARD_MAGIC);
+	if (p15card == NULL || p15card->magic != SC_PKCS15_CARD_MAGIC) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	switch (df->type) {
 	case SC_PKCS15_PRKDF:
 		func = sc_pkcs15_encode_prkdf_entry;
@@ -2309,7 +2314,9 @@ sc_pkcs15_read_file(struct sc_pkcs15_card *p15card, const struct sc_path *in_pat
 	size_t	len = 0, offset = 0;
 	int	r;
 
-	assert(p15card != NULL && in_path != NULL && buf != NULL);
+	if (p15card == NULL || in_path == NULL || buf == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	LOG_FUNC_CALLED(ctx);
 	sc_log(ctx, "path=%s, index=%u, count=%d", sc_print_path(in_path), in_path->index, in_path->count);
@@ -2420,7 +2427,8 @@ fail_unlock:
 int
 sc_pkcs15_compare_id(const struct sc_pkcs15_id *id1, const struct sc_pkcs15_id *id2)
 {
-	assert(id1 != NULL && id2 != NULL);
+	if (id1 == NULL || id2 == NULL)
+		return 0;
 	if (id1->len != id2->len)
 		return 0;
 	return memcmp(id1->value, id2->value, id1->len) == 0;
diff --git a/src/libopensc/sc.c b/src/libopensc/sc.c
index 306ec57..051fcf6 100644
--- a/src/libopensc/sc.c
+++ b/src/libopensc/sc.c
@@ -52,7 +52,9 @@ int sc_hex_to_bin(const char *in, u8 *out, size_t *outlen)
 	int err = SC_SUCCESS;
 	size_t left, count = 0, in_len;
 
-	assert(in != NULL && out != NULL && outlen != NULL);
+	if (in == NULL || out == NULL || outlen == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	left = *outlen;
 	in_len = strlen(in);
 
@@ -233,7 +235,9 @@ int sc_compare_oid(const struct sc_object_id *oid1, const struct sc_object_id *o
 {
 	int i;
 
-	assert(oid1 != NULL && oid2 != NULL);
+	if (oid1 == NULL || oid2 == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	for (i = 0; i < SC_MAX_OBJECT_ID_OCTETS; i++)   {
 		if (oid1->value[i] != oid2->value[i])
@@ -432,8 +436,9 @@ int sc_file_add_acl_entry(sc_file_t *file, unsigned int operation,
 {
 	sc_acl_entry_t *p, *_new;
 
-	assert(file != NULL);
-	assert(operation < SC_MAX_AC_OPS);
+	if (file == NULL || operation >= SC_MAX_AC_OPS) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	switch (method) {
 	case SC_AC_NEVER:
@@ -499,8 +504,9 @@ const sc_acl_entry_t * sc_file_get_acl_entry(const sc_file_t *file,
 		SC_AC_UNKNOWN, SC_AC_KEY_REF_NONE, {{0, 0, 0, {0}}}, NULL
 	};
 
-	assert(file != NULL);
-	assert(operation < SC_MAX_AC_OPS);
+	if (file == NULL || operation >= SC_MAX_AC_OPS) {
+		return NULL;
+	}
 
 	p = file->acl[operation];
 	if (p == (sc_acl_entry_t *) 1)
@@ -517,8 +523,9 @@ void sc_file_clear_acl_entries(sc_file_t *file, unsigned int operation)
 {
 	sc_acl_entry_t *e;
 
-	assert(file != NULL);
-	assert(operation < SC_MAX_AC_OPS);
+	if (file == NULL || operation >= SC_MAX_AC_OPS) {
+		return;
+	}
 
 	e = file->acl[operation];
 	if (e == (sc_acl_entry_t *) 1 ||
@@ -549,9 +556,8 @@ sc_file_t * sc_file_new(void)
 void sc_file_free(sc_file_t *file)
 {
 	unsigned int i;
-	if (file == NULL)
+	if (file == NULL || !sc_file_valid(file))
 		return;
-	assert(sc_file_valid(file));
 	file->magic = 0;
 	for (i = 0; i < SC_MAX_AC_OPS; i++)
 		sc_file_clear_acl_entries(file, i);
@@ -572,7 +578,8 @@ void sc_file_dup(sc_file_t **dest, const sc_file_t *src)
 	const sc_acl_entry_t *e;
 	unsigned int op;
 
-	assert(sc_file_valid(src));
+	if (!sc_file_valid(src))
+		return;
 	*dest = NULL;
 	newf = sc_file_new();
 	if (newf == NULL)
@@ -617,7 +624,9 @@ int sc_file_set_sec_attr(sc_file_t *file, const u8 *sec_attr,
 			 size_t sec_attr_len)
 {
 	u8 *tmp;
-	assert(sc_file_valid(file));
+	if (!sc_file_valid(file)) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	if (sec_attr == NULL) {
 		if (file->sec_attr != NULL)
@@ -645,7 +654,9 @@ int sc_file_set_prop_attr(sc_file_t *file, const u8 *prop_attr,
 			 size_t prop_attr_len)
 {
 	u8 *tmp;
-	assert(sc_file_valid(file));
+	if (!sc_file_valid(file)) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	if (prop_attr == NULL) {
 		if (file->prop_attr != NULL)
@@ -673,7 +684,9 @@ int sc_file_set_type_attr(sc_file_t *file, const u8 *type_attr,
 			 size_t type_attr_len)
 {
 	u8 *tmp;
-	assert(sc_file_valid(file));
+	if (!sc_file_valid(file)) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	if (type_attr == NULL) {
 		if (file->type_attr != NULL)
@@ -702,7 +715,9 @@ int sc_file_set_content(sc_file_t *file, const u8 *content,
 			 size_t content_len)
 {
 	u8 *tmp;
-	assert(sc_file_valid(file));
+	if (!sc_file_valid(file)) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 
 	if (content == NULL) {
 		if (file->encoded_content != NULL)
@@ -730,9 +745,8 @@ int sc_file_set_content(sc_file_t *file, const u8 *content,
 
 
 int sc_file_valid(const sc_file_t *file) {
-#ifndef NDEBUG
-	assert(file != NULL);
-#endif
+	if (file == NULL)
+		return 0;
 	return file->magic == SC_FILE_MAGIC;
 }
 
diff --git a/src/libopensc/sec.c b/src/libopensc/sec.c
index 140c562..024b216 100644
--- a/src/libopensc/sec.c
+++ b/src/libopensc/sec.c
@@ -36,7 +36,9 @@ int sc_decipher(sc_card_t *card,
 {
 	int r;
 
-	assert(card != NULL && crgram != NULL && out != NULL);
+	if (card == NULL || crgram == NULL || out == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_NORMAL);
 	if (card->ops->decipher == NULL)
 		SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_NOT_SUPPORTED);
@@ -50,7 +52,9 @@ int sc_compute_signature(sc_card_t *card,
 {
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_NORMAL);
 	if (card->ops->compute_signature == NULL)
 		SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_NOT_SUPPORTED);
@@ -64,7 +68,9 @@ int sc_set_security_env(sc_card_t *card,
 {
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_NORMAL);
 	if (card->ops->set_security_env == NULL)
 		SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_NOT_SUPPORTED);
@@ -76,7 +82,9 @@ int sc_restore_security_env(sc_card_t *card, int se_num)
 {
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_NORMAL);
 	if (card->ops->restore_security_env == NULL)
 		SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_NOT_SUPPORTED);
@@ -155,7 +163,9 @@ int sc_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data,
 {
 	int r;
 
-	assert(card != NULL);
+	if (card == NULL) {
+		return SC_ERROR_INVALID_ARGUMENTS;
+	}
 	SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_NORMAL);
 	if (card->ops->pin_cmd) {
 		r = card->ops->pin_cmd(card, data, tries_left);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-opensc/opensc.git



More information about the pkg-opensc-commit mailing list