diff --git a/src/common/compat_getopt_main.c b/src/common/compat_getopt_main.c index b5215118..827ba019 100644 --- a/src/common/compat_getopt_main.c +++ b/src/common/compat_getopt_main.c @@ -234,7 +234,7 @@ main(int argc, char * argv[]) case 'o': /* -output=FILE */ outfilename = optarg; /* we allow "-" as a synonym for stdout here */ - if (! strcmp(optarg, "-")) + if (optarg && !strcmp(optarg, "-")) { outfilename = 0; } diff --git a/src/libopensc/card-authentic.c b/src/libopensc/card-authentic.c index 744c534b..737574cd 100644 --- a/src/libopensc/card-authentic.c +++ b/src/libopensc/card-authentic.c @@ -650,7 +650,7 @@ authentic_reduce_path(struct sc_card *card, struct sc_path *path) LOG_FUNC_CALLED(ctx); - if (path->len <= 2 || path->type == SC_PATH_TYPE_DF_NAME || !path) + if (!path || path->len <= 2 || path->type == SC_PATH_TYPE_DF_NAME) LOG_FUNC_RETURN(ctx, SC_SUCCESS); if (!card->cache.valid || !card->cache.current_df) diff --git a/src/libopensc/card-iasecc.c b/src/libopensc/card-iasecc.c index 6e26c87d..318ff548 100644 --- a/src/libopensc/card-iasecc.c +++ b/src/libopensc/card-iasecc.c @@ -2565,7 +2565,8 @@ iasecc_sdo_create(struct sc_card *card, struct iasecc_sdo *sdo) rv = iasecc_sdo_put_data(card, &update); LOG_TEST_RET(ctx, rv, "failed to update 'Compulsory usage' data"); - field->on_card = 1; + if (field) + field->on_card = 1; } free(data); @@ -3252,14 +3253,19 @@ static int iasecc_compute_signature(struct sc_card *card, const unsigned char *in, size_t in_len, unsigned char *out, size_t out_len) { - struct sc_context *ctx = card->ctx; - struct iasecc_private_data *prv = (struct iasecc_private_data *) card->drv_data; - struct sc_security_env *env = &prv->security_env; + struct sc_context *ctx; + struct iasecc_private_data *prv; + struct sc_security_env *env; + + if (!card || !in || !out) + return SC_ERROR_INVALID_ARGUMENTS; + + ctx = card->ctx; + prv = (struct iasecc_private_data *) card->drv_data; + env = &prv->security_env; LOG_FUNC_CALLED(ctx); sc_log(ctx, "inlen %i, outlen %i", in_len, out_len); - if (!card || !in || !out) - LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Invalid compute signature arguments"); if (env->operation == SC_SEC_OPERATION_SIGN) return iasecc_compute_signature_dst(card, in, in_len, out, out_len); diff --git a/src/libopensc/card-oberthur.c b/src/libopensc/card-oberthur.c index 2a9dbe3f..eb0820b1 100644 --- a/src/libopensc/card-oberthur.c +++ b/src/libopensc/card-oberthur.c @@ -1108,16 +1108,17 @@ auth_compute_signature(struct sc_card *card, const unsigned char *in, size_t ile unsigned char resp[SC_MAX_APDU_BUFFER_SIZE]; int rv; - SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE); - sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "inlen %i, outlen %i\n", ilen, olen); if (!card || !in || !out) { - SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_INVALID_ARGUMENTS); + return SC_ERROR_INVALID_ARGUMENTS; } else if (ilen > 96) { sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Illegal input length %d\n", ilen); SC_TEST_RET(card->ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_INVALID_ARGUMENTS, "Illegal input length"); } + SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE); + sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "inlen %i, outlen %i\n", ilen, olen); + sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x9E, 0x9A); apdu.datalen = ilen; apdu.data = in; diff --git a/src/libopensc/card-westcos.c b/src/libopensc/card-westcos.c index 1642eb7d..60bcb999 100644 --- a/src/libopensc/card-westcos.c +++ b/src/libopensc/card-westcos.c @@ -1109,6 +1109,15 @@ static int westcos_sign_decipher(int mode, sc_card_t *card, return SC_ERROR_INVALID_ARGUMENTS; sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "westcos_sign_decipher outlen=%d\n", outlen); + +#ifndef ENABLE_OPENSSL + r = SC_ERROR_NOT_SUPPORTED; +#else + if (keyfile == NULL || mem == NULL || card->drv_data == NULL) { + r = SC_ERROR_OUT_OF_MEMORY; + goto out; + } + priv_data = (priv_data_t *) card->drv_data; if(priv_data->flags & RSA_CRYPTO_COMPONENT) @@ -1134,14 +1143,6 @@ static int westcos_sign_decipher(int mode, sc_card_t *card, r = apdu.resplen; goto out2; } - -#ifndef ENABLE_OPENSSL - r = SC_ERROR_NOT_SUPPORTED; -#else - if (keyfile == NULL || mem == NULL || priv_data == NULL) { - r = SC_ERROR_OUT_OF_MEMORY; - goto out; - } if ((priv_data->env.flags) & SC_ALGORITHM_RSA_PAD_PKCS1) pad = RSA_PKCS1_PADDING; diff --git a/src/libopensc/card.c b/src/libopensc/card.c index 9a331728..5142d0c0 100644 --- a/src/libopensc/card.c +++ b/src/libopensc/card.c @@ -403,10 +403,11 @@ int sc_create_file(sc_card_t *card, sc_file_t *file) { int r; char pbuf[SC_MAX_PATH_STRING_SIZE]; - const sc_path_t *in_path = &file->path; + const sc_path_t *in_path; - assert(card != NULL); + assert(card != NULL && file != NULL); + in_path = &file->path; r = sc_path_print(pbuf, sizeof(pbuf), in_path); if (r != SC_SUCCESS) pbuf[0] = '\0'; @@ -864,14 +865,16 @@ sc_algorithm_info_t * sc_card_find_gostr3410_alg(sc_card_t *card, static int match_atr_table(sc_context_t *ctx, struct sc_atr_table *table, struct sc_atr *atr) { - u8 *card_atr_bin = atr->value; - size_t card_atr_bin_len = atr->len; + u8 *card_atr_bin; + size_t card_atr_bin_len; char card_atr_hex[3 * SC_MAX_ATR_SIZE]; size_t card_atr_hex_len; unsigned int i = 0; if (ctx == NULL || table == NULL || atr == NULL) return -1; + card_atr_bin = atr->value; + card_atr_bin_len = atr->len; sc_bin_to_hex(card_atr_bin, card_atr_bin_len, card_atr_hex, sizeof(card_atr_hex), ':'); card_atr_hex_len = strlen(card_atr_hex); diff --git a/src/libopensc/cwa14890.c b/src/libopensc/cwa14890.c index dbbd599b..c9515275 100644 --- a/src/libopensc/cwa14890.c +++ b/src/libopensc/cwa14890.c @@ -1434,10 +1434,6 @@ int cwa_encode_apdu(sc_card_t * card, u8 *msgbuf = NULL; /* to encrypt apdu data */ u8 *cryptbuf = NULL; - /* reserve extra bytes for padding and tlv header */ - msgbuf = calloc(12 + from->lc, sizeof(u8)); /* to encrypt apdu data */ - cryptbuf = calloc(12 + from->lc, sizeof(u8)); - /* mandatory check */ if (!card || !card->ctx || !provider) return SC_ERROR_INVALID_ARGUMENTS; @@ -1450,6 +1446,10 @@ int cwa_encode_apdu(sc_card_t * card, LOG_FUNC_RETURN(ctx, SC_ERROR_SM_NOT_INITIALIZED); if (sm_session->state != CWA_SM_ACTIVE) LOG_FUNC_RETURN(ctx, SC_ERROR_SM_INVALID_LEVEL); + + /* reserve extra bytes for padding and tlv header */ + msgbuf = calloc(12 + from->lc, sizeof(u8)); /* to encrypt apdu data */ + cryptbuf = calloc(12 + from->lc, sizeof(u8)); if (!msgbuf || !cryptbuf) LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); diff --git a/src/libopensc/sc.c b/src/libopensc/sc.c index a2be9b34..08375f0c 100644 --- a/src/libopensc/sc.c +++ b/src/libopensc/sc.c @@ -274,17 +274,18 @@ void sc_format_path(const char *str, sc_path_t *path) { int type = SC_PATH_TYPE_PATH; - memset(path, 0, sizeof(*path)); - if (*str == 'i' || *str == 'I') { - type = SC_PATH_TYPE_FILE_ID; - str++; + if (path) { + memset(path, 0, sizeof(*path)); + if (*str == 'i' || *str == 'I') { + type = SC_PATH_TYPE_FILE_ID; + str++; + } + path->len = sizeof(path->value); + if (sc_hex_to_bin(str, path->value, &path->len) >= 0) { + path->type = type; + } + path->count = -1; } - path->len = sizeof(path->value); - if (sc_hex_to_bin(str, path->value, &path->len) >= 0) { - path->type = type; - } - path->count = -1; - return; } int sc_append_path(sc_path_t *dest, const sc_path_t *src) diff --git a/src/pkcs11/framework-pkcs15.c b/src/pkcs11/framework-pkcs15.c index c30167e5..2bf63774 100644 --- a/src/pkcs11/framework-pkcs15.c +++ b/src/pkcs11/framework-pkcs15.c @@ -3446,10 +3446,12 @@ pkcs15_prkey_get_attribute(struct sc_pkcs11_session *session, check_attribute_buffer(attr, sizeof(CK_ULONG)); switch (prkey->prv_p15obj->type) { case SC_PKCS15_TYPE_PRKEY_EC: - if (key && key->u.ec.params.field_length > 0) - *(CK_ULONG *) attr->pValue = key->u.ec.params.field_length; - else - *(CK_ULONG *) attr->pValue = (key->u.ec.ecpointQ.len - 1) / 2 *8; + if (key) { + if (key->u.ec.params.field_length > 0) + *(CK_ULONG *) attr->pValue = key->u.ec.params.field_length; + else + *(CK_ULONG *) attr->pValue = (key->u.ec.ecpointQ.len - 1) / 2 *8; + } return CKR_OK; default: *(CK_ULONG *) attr->pValue = prkey->prv_info->modulus_length; diff --git a/src/pkcs11/pkcs11-global.c b/src/pkcs11/pkcs11-global.c index 64abd1a7..aa507589 100644 --- a/src/pkcs11/pkcs11-global.c +++ b/src/pkcs11/pkcs11-global.c @@ -688,7 +688,7 @@ out: sc_wait_for_event(context, 0, NULL, NULL, -1, &reader_states); } - sc_log(context, "C_WaitForSlotEvent() = %s, event in 0x%lx", lookup_enum (RV_T, rv), *pSlot); + sc_log(context, "C_WaitForSlotEvent() = %s", lookup_enum (RV_T, rv)); sc_pkcs11_unlock(); return rv; } diff --git a/src/pkcs15init/pkcs15-entersafe.c b/src/pkcs15init/pkcs15-entersafe.c index 80c66698..12611179 100644 --- a/src/pkcs15init/pkcs15-entersafe.c +++ b/src/pkcs15init/pkcs15-entersafe.c @@ -292,10 +292,9 @@ static int entersafe_create_pin(sc_profile_t *profile, sc_pkcs15_card_t *p15card data.key_data.symmetric.key_len=16; r = sc_card_ctl(card, SC_CARDCTL_ENTERSAFE_WRITE_KEY, &data); - if (pin_obj) { - /* Cache new PIN value. */ - sc_pkcs15_pincache_add(p15card, pin_obj, pin, pin_len); - } + + /* Cache new PIN value. */ + sc_pkcs15_pincache_add(p15card, pin_obj, pin, pin_len); } {/*puk*/ diff --git a/src/pkcs15init/pkcs15-epass2003.c b/src/pkcs15init/pkcs15-epass2003.c index a457ec93..d35cad63 100644 --- a/src/pkcs15init/pkcs15-epass2003.c +++ b/src/pkcs15init/pkcs15-epass2003.c @@ -470,8 +470,7 @@ static int epass2003_pkcs15_store_key(struct sc_profile *profile, SC_TEST_RET(card->ctx, SC_LOG_DEBUG_NORMAL, r, "store key: cannot update private key"); - if (file) - sc_file_free(file); + sc_file_free(file); SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r); } diff --git a/src/pkcs15init/pkcs15-gpk.c b/src/pkcs15init/pkcs15-gpk.c index fdcf1871..e6502d96 100644 --- a/src/pkcs15init/pkcs15-gpk.c +++ b/src/pkcs15init/pkcs15-gpk.c @@ -458,8 +458,7 @@ gpk_create_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_pkcs15_objec #endif done: - if (keyfile) - sc_file_free(keyfile); + sc_file_free(keyfile); return r; } diff --git a/src/pkcs15init/pkcs15-jcop.c b/src/pkcs15init/pkcs15-jcop.c index b20e09f2..31854529 100644 --- a/src/pkcs15init/pkcs15-jcop.c +++ b/src/pkcs15init/pkcs15-jcop.c @@ -140,10 +140,10 @@ jcop_create_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_pkcs15_obje size_t bytes, mod_len, prv_len; int r; - if (obj->type != SC_PKCS15_TYPE_PRKEY_RSA) { - sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, "JCOP supports only RSA keys."); - return SC_ERROR_NOT_SUPPORTED; - } + if (obj->type != SC_PKCS15_TYPE_PRKEY_RSA) { + sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, "JCOP supports only RSA keys."); + return SC_ERROR_NOT_SUPPORTED; + } /* The caller is supposed to have chosen a key file path for us */ if (key_info->path.len == 0 || key_info->modulus_length == 0) return SC_ERROR_INVALID_ARGUMENTS; @@ -155,7 +155,7 @@ jcop_create_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_pkcs15_obje mod_len = key_info->modulus_length / 8; bytes = mod_len / 2; - prv_len = 2 + 5 * bytes; + prv_len = 2 + 5 * bytes; keyfile->size = prv_len; /* Fix up PIN references in file ACL */ @@ -164,8 +164,7 @@ jcop_create_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_pkcs15_obje if (r >= 0) r = sc_pkcs15init_create_file(profile, p15card, keyfile); - if (keyfile) - sc_file_free(keyfile); + sc_file_free(keyfile); return r; } diff --git a/src/pkcs15init/pkcs15-lib.c b/src/pkcs15init/pkcs15-lib.c index 913450a2..02422c73 100644 --- a/src/pkcs15init/pkcs15-lib.c +++ b/src/pkcs15init/pkcs15-lib.c @@ -1888,8 +1888,7 @@ sc_pkcs15init_store_data(struct sc_pkcs15_card *p15card, struct sc_profile *prof *path = file->path; - if (file) - sc_file_free(file); + sc_file_free(file); LOG_FUNC_RETURN(ctx, r); } @@ -2197,7 +2196,7 @@ sc_pkcs15init_select_intrinsic_id(struct sc_pkcs15_card *p15card, struct sc_prof { struct sc_context *ctx = p15card->card->ctx; struct sc_pkcs15_pubkey *pubkey = NULL; - unsigned id_style = profile->id_style; + unsigned id_style; struct sc_pkcs15_id id; unsigned char *id_data = NULL; size_t id_data_len = 0; @@ -2207,15 +2206,17 @@ sc_pkcs15init_select_intrinsic_id(struct sc_pkcs15_card *p15card, struct sc_prof #ifndef ENABLE_OPENSSL LOG_FUNC_RETURN(ctx, SC_SUCCESS); #else - if (!id_out) + if (!id_out || !profile) LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS); + id_style = profile->id_style; + /* ID already exists */ if (id_out->len) LOG_FUNC_RETURN(ctx, SC_SUCCESS); /* Native ID style is not intrisic one */ - if (profile->id_style == SC_PKCS15INIT_ID_STYLE_NATIVE) + if (id_style == SC_PKCS15INIT_ID_STYLE_NATIVE) LOG_FUNC_RETURN(ctx, SC_SUCCESS); memset(&id, 0, sizeof(id)); @@ -2283,7 +2284,7 @@ sc_pkcs15init_select_intrinsic_id(struct sc_pkcs15_card *p15card, struct sc_prof break; default: - sc_log(ctx, "Unsupported ID style: %i", profile->id_style); + sc_log(ctx, "Unsupported ID style: %i", id_style); LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Non supported ID style"); } @@ -3238,25 +3239,23 @@ sc_pkcs15init_verify_secret(struct sc_profile *profile, struct sc_pkcs15_card *p sc_log(ctx, "Symbolic PIN resolved to PIN(type:CHV,reference:%i)", type, reference); } - if (p15card) { - if (path && path->len) { - struct sc_path tmp_path = *path; - int iter; + if (path && path->len) { + struct sc_path tmp_path = *path; + int iter; - r = SC_ERROR_OBJECT_NOT_FOUND; - for (iter = tmp_path.len/2; iter >= 0 && r == SC_ERROR_OBJECT_NOT_FOUND; iter--, tmp_path.len -= 2) - r = sc_pkcs15_find_pin_by_type_and_reference(p15card, - tmp_path.len ? &tmp_path : NULL, - type, reference, &pin_obj); - } - else { - r = sc_pkcs15_find_pin_by_type_and_reference(p15card, NULL, type, reference, &pin_obj); - } + r = SC_ERROR_OBJECT_NOT_FOUND; + for (iter = tmp_path.len/2; iter >= 0 && r == SC_ERROR_OBJECT_NOT_FOUND; iter--, tmp_path.len -= 2) + r = sc_pkcs15_find_pin_by_type_and_reference(p15card, + tmp_path.len ? &tmp_path : NULL, + type, reference, &pin_obj); + } + else { + r = sc_pkcs15_find_pin_by_type_and_reference(p15card, NULL, type, reference, &pin_obj); + } - if (!r && pin_obj) { - memcpy(&auth_info, pin_obj->data, sizeof(auth_info)); - sc_log(ctx, "found PIN object '%s'", pin_obj->label); - } + if (!r && pin_obj) { + memcpy(&auth_info, pin_obj->data, sizeof(auth_info)); + sc_log(ctx, "found PIN object '%s'", pin_obj->label); } if (pin_obj) { diff --git a/src/pkcs15init/pkcs15-myeid.c b/src/pkcs15init/pkcs15-myeid.c index 4ade6974..1da4515a 100644 --- a/src/pkcs15init/pkcs15-myeid.c +++ b/src/pkcs15init/pkcs15-myeid.c @@ -189,7 +189,7 @@ myeid_init_card(sc_profile_t *profile, */ static int myeid_create_dir(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_file_t *df) { - struct sc_context *ctx = p15card->card->ctx; + struct sc_context *ctx; struct sc_file *file = NULL; int r = 0, ii; static const char *create_dfs[] = { @@ -209,9 +209,10 @@ myeid_create_dir(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_file_t *df SC_PKCS15_DODF }; - if (!profile || !p15card || !df) + if (!profile || !p15card || !p15card->card || !df) return SC_ERROR_INVALID_ARGUMENTS; + ctx = p15card->card->ctx; LOG_FUNC_CALLED(ctx); sc_log(ctx, "id (%x)", df->id); diff --git a/src/pkcs15init/pkcs15-oberthur-awp.c b/src/pkcs15init/pkcs15-oberthur-awp.c index 18032fba..aa4e20a1 100644 --- a/src/pkcs15init/pkcs15-oberthur-awp.c +++ b/src/pkcs15init/pkcs15-oberthur-awp.c @@ -330,12 +330,10 @@ awp_create_container(struct sc_pkcs15_card *p15card, struct sc_profile *profile, rv = awp_create_container_record(p15card, profile, file, acc); - if (clist) - sc_file_free(clist); - if (file) - sc_file_free(file); if (list) free(list); + sc_file_free(file); + sc_file_free(clist); SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, rv); } @@ -1669,8 +1667,8 @@ awp_delete_from_container(struct sc_pkcs15_card *p15card, rv = 0; if (buff) free(buff); - if (file) sc_file_free(file); if (clist) sc_file_free(clist); + sc_file_free(file); SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, rv); } @@ -1744,8 +1742,7 @@ awp_remove_from_object_list( struct sc_pkcs15_card *p15card, struct sc_profile * done: if (buff) free(buff); - if (lst) - sc_file_free(lst); + sc_file_free(lst); if (lst_file) sc_file_free(lst_file); diff --git a/src/pkcs15init/pkcs15-oberthur.c b/src/pkcs15init/pkcs15-oberthur.c index 34185987..15661e73 100644 --- a/src/pkcs15init/pkcs15-oberthur.c +++ b/src/pkcs15init/pkcs15-oberthur.c @@ -63,15 +63,17 @@ static int cosm_write_tokeninfo (struct sc_pkcs15_card *p15card, struct sc_profile *profile, char *label, unsigned flags) { - struct sc_context *ctx = p15card->card->ctx; + struct sc_context *ctx; struct sc_file *file = NULL; int rv; size_t sz; char *buffer = NULL; - if (!p15card || !profile) + if (!p15card || !p15card->card || !profile) return SC_ERROR_INVALID_ARGUMENTS; + ctx = p15card->card->ctx; + SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE); sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "cosm_write_tokeninfo() label '%s'; flags 0x%X", label, flags); if (sc_profile_get_file(profile, COSM_TITLE"-token-info", &file)) diff --git a/src/pkcs15init/pkcs15-setcos.c b/src/pkcs15init/pkcs15-setcos.c index fdd185ce..d2e85f17 100644 --- a/src/pkcs15init/pkcs15-setcos.c +++ b/src/pkcs15init/pkcs15-setcos.c @@ -236,8 +236,7 @@ setcos_create_pin(sc_profile_t *profile, sc_pkcs15_card_t *p15card, SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, r, "Cannot set MF into the activated state"); } - if(pinfile) - sc_file_free(pinfile); + sc_file_free(pinfile); SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, r); } diff --git a/src/pkcs15init/profile.c b/src/pkcs15init/profile.c index 1c560f89..433c7ae4 100644 --- a/src/pkcs15init/profile.c +++ b/src/pkcs15init/profile.c @@ -2059,7 +2059,7 @@ sc_profile_find_file_by_path(struct sc_profile *pro, const sc_path_t *path) sc_log(ctx, "find profile file by path:%s", sc_print_path(path)); #endif - if (!path->len && !path->aid.len) + if (!path || (!path->len && !path->aid.len)) return NULL; for (fi = pro->ef_list; fi; fi = fi->next) { diff --git a/src/scconf/parse.c b/src/scconf/parse.c index 2a9aed3f..043ab794 100644 --- a/src/scconf/parse.c +++ b/src/scconf/parse.c @@ -82,8 +82,9 @@ static scconf_item *scconf_item_find(scconf_parser * parser) scconf_item *item; for (item = parser->block->items; item; item = item->next) { - if (item->type == SCCONF_ITEM_TYPE_VALUE && - strcasecmp(item->key, parser->key) == 0) { + if (item && item->type == SCCONF_ITEM_TYPE_VALUE + && item->key && parser->key + && strcasecmp(item->key, parser->key) == 0) { return item; } } @@ -148,20 +149,24 @@ scconf_item *scconf_item_add(scconf_context * config, scconf_block * block, scco scconf_list_copy(dst->name, &parser.name); } scconf_item_add_internal(&parser, type); - switch (parser.current_item->type) { - case SCCONF_ITEM_TYPE_COMMENT: - parser.current_item->value.comment = strdup((const char *) data); - break; - case SCCONF_ITEM_TYPE_BLOCK: - if (!dst) - return NULL; - dst->parent = parser.block; - parser.current_item->value.block = dst; - scconf_list_destroy(parser.name); - break; - case SCCONF_ITEM_TYPE_VALUE: - scconf_list_copy((const scconf_list *) data, &parser.current_item->value.list); - break; + if (parser.current_item) { + switch (parser.current_item->type) { + case SCCONF_ITEM_TYPE_COMMENT: + parser.current_item->value.comment = strdup((const char *) data); + break; + case SCCONF_ITEM_TYPE_BLOCK: + if (!dst) + return NULL; + dst->parent = parser.block; + parser.current_item->value.block = dst; + scconf_list_destroy(parser.name); + break; + case SCCONF_ITEM_TYPE_VALUE: + scconf_list_copy((const scconf_list *) data, &parser.current_item->value.list); + break; + } + } else { + /* FIXME is it an error if item is NULL? */ } return parser.current_item; } diff --git a/src/scconf/sclex.c b/src/scconf/sclex.c index b16db4b7..c5a6758f 100644 --- a/src/scconf/sclex.c +++ b/src/scconf/sclex.c @@ -57,8 +57,10 @@ static void buf_addch(BUFHAN * bp, char ch) bp->bufmax += 256; bp->buf = (char *) realloc(bp->buf, bp->bufmax); } - bp->buf[bp->bufcur++] = ch; - bp->buf[bp->bufcur] = '\0'; + if (bp->buf) { + bp->buf[bp->bufcur++] = ch; + bp->buf[bp->bufcur] = '\0'; + } } static int buf_nextch(BUFHAN * bp) diff --git a/src/tests/base64.c b/src/tests/base64.c index ec9583dc..f868d12d 100644 --- a/src/tests/base64.c +++ b/src/tests/base64.c @@ -38,6 +38,7 @@ int main(int argc, char *argv[]) fwrite(outbuf, len, 1, stdout); r = 0; err: - fclose(inf); + if (inf) + fclose(inf); return r; } diff --git a/src/tools/dnie-tool.c b/src/tools/dnie-tool.c index 93f7c108..c856a7ba 100644 --- a/src/tools/dnie-tool.c +++ b/src/tools/dnie-tool.c @@ -146,7 +146,7 @@ int main(int argc, char* argv[]) if (r) { fprintf(stderr, "Error: Failed to establish context: %s\n", sc_strerror(r)); - return -1; + goto dnie_tool_end; } if (verbose > 1) { diff --git a/src/tools/piv-tool.c b/src/tools/piv-tool.c index c8455317..596b5a44 100644 --- a/src/tools/piv-tool.c +++ b/src/tools/piv-tool.c @@ -113,7 +113,7 @@ static int load_object(const char * object_id, const char * object_file) int r; struct stat stat_buf; - if((fp=fopen(object_file, "r"))==NULL){ + if(!object_file || (fp=fopen(object_file, "r")) == NULL){ printf("Cannot open object file, %s %s\n", (object_file)?object_file:"", strerror(errno)); return -1; diff --git a/src/tools/pkcs15-init.c b/src/tools/pkcs15-init.c index aa3474a6..21abbbf4 100644 --- a/src/tools/pkcs15-init.c +++ b/src/tools/pkcs15-init.c @@ -1296,8 +1296,7 @@ static int get_cert_info(sc_pkcs15_card_t *myp15card, sc_pkcs15_object_t *certob } done: - if (cert) - sc_pkcs15_free_certificate(cert); + sc_pkcs15_free_certificate(cert); if (othercert) sc_pkcs15_free_certificate(othercert); diff --git a/src/tools/pkcs15-tool.c b/src/tools/pkcs15-tool.c index 603a7534..b357319a 100644 --- a/src/tools/pkcs15-tool.c +++ b/src/tools/pkcs15-tool.c @@ -783,17 +783,17 @@ static int read_ssh_key(void) struct sc_pkcs15_object *obj = NULL; sc_pkcs15_pubkey_t *pubkey = NULL; sc_pkcs15_cert_t *cert = NULL; - FILE *outf = NULL; + FILE *outf = NULL; - if (opt_outfile != NULL) { - outf = fopen(opt_outfile, "w"); - if (outf == NULL) { - fprintf(stderr, "Error opening file '%s': %s\n", opt_outfile, strerror(errno)); - goto fail2; - } - } + if (opt_outfile != NULL) { + outf = fopen(opt_outfile, "w"); + if (outf == NULL) { + fprintf(stderr, "Error opening file '%s': %s\n", opt_outfile, strerror(errno)); + goto fail2; + } + } else { - outf = stdout; + outf = stdout; } id.len = SC_PKCS15_MAX_ID_SIZE; @@ -988,8 +988,8 @@ static int read_ssh_key(void) free(uu); } - if (outf != stdout) - fclose(outf); + if (outf != stdout) + fclose(outf); if (cert) sc_pkcs15_free_certificate(cert); else if (pubkey) @@ -999,8 +999,8 @@ static int read_ssh_key(void) fail: printf("can't convert key: buffer too small\n"); fail2: - if (outf != stdout) - fclose(outf); + if (outf && outf != stdout) + fclose(outf); if (cert) sc_pkcs15_free_certificate(cert); else if (pubkey) diff --git a/src/tools/westcos-tool.c b/src/tools/westcos-tool.c index 54a5d65d..24136fbb 100644 --- a/src/tools/westcos-tool.c +++ b/src/tools/westcos-tool.c @@ -305,7 +305,7 @@ static int cert2der(X509 *cert, u8 **value) static int create_file_cert(sc_card_t *card) { int r; - int size; + int size = 0; sc_path_t path; sc_file_t *file = NULL; @@ -313,12 +313,13 @@ static int create_file_cert(sc_card_t *card) r = sc_select_file(card, &path, &file); if(r) goto out; - size = (file->size) - 32; - if(file) { + size = (file->size) - 32; sc_file_free(file); file = NULL; + } else { + size = 2048; } sc_format_path("0002", &path); @@ -903,8 +904,7 @@ out: sc_disconnect_card(card); } - if (ctx) - sc_release_context(ctx); + sc_release_context(ctx); return EXIT_SUCCESS; }