From 50e266567aa5a81fc84f3d2b812a24cc0c637e07 Mon Sep 17 00:00:00 2001 From: aet Date: Fri, 22 Mar 2002 00:13:25 +0000 Subject: [PATCH] Fix memory leaks Indent lines git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@369 c6295689-39f2-0310-b995-f0e70906c6a9 --- src/tests/base64.c | 2 +- src/tests/lottery.c | 47 +++++++++++------------- src/tests/p15dump.c | 21 +++++------ src/tests/pintest.c | 52 ++++++++++++++------------ src/tests/print.c | 87 ++++++++++++++++++++++---------------------- src/tests/prngtest.c | 14 +++---- src/tests/sc-test.c | 2 +- src/tests/sc-test.h | 2 +- 8 files changed, 112 insertions(+), 115 deletions(-) diff --git a/src/tests/base64.c b/src/tests/base64.c index f5108ab0..03d1f062 100644 --- a/src/tests/base64.c +++ b/src/tests/base64.c @@ -1,5 +1,5 @@ -#include #include +#include #include int main(int argc, char *argv[]) diff --git a/src/tests/lottery.c b/src/tests/lottery.c index 8a6e7ca9..859faf00 100644 --- a/src/tests/lottery.c +++ b/src/tests/lottery.c @@ -2,30 +2,28 @@ * All rights reserved. */ -#include "sc-test.h" -#include "opensc.h" -#include #include +#include #include #include +#include +#include "sc-test.h" int main(int argc, char *argv[]) { - int i, c, r; - int freq[39]; + int i, c, r, freq[39]; struct timeval tv1, tv2; u8 buf[14]; i = sc_test_init(&argc, argv); - for (i = 0; i < 39; i++) freq[i] = 0; c = 0; while (1) { - u8 alkiot[39]; - + u8 nbuf[39]; + for (i = 0; i < 39; i++) { - alkiot[i] = i + 1; + nbuf[i] = i + 1; } if (c == 0) gettimeofday(&tv1, NULL); @@ -33,38 +31,37 @@ int main(int argc, char *argv[]) r = sc_get_challenge(card, buf, 14); sc_unlock(card); if (r == 0) { - int i, jaljella = 39; - - printf("Lottorivi: "); + int i, left = 39; + + printf("Lottery: "); for (i = 0; i < 7; i++) { - unsigned short s = buf[2*i] + (buf[2*i+1] << 8); - int lot = s % (jaljella+1); - int num = alkiot[lot]; - - alkiot[lot] = alkiot[jaljella-1]; - jaljella--; - - freq[num-1]++; - + unsigned short s = buf[2 * i] + (buf[2 * i + 1] << 8); + int lot = s % (left + 1); + int num = nbuf[lot]; + + nbuf[lot] = nbuf[left - 1]; + left--; + freq[num - 1]++; printf("%3d ", num); } printf("\n"); } else { - fprintf(stderr, "get_random() failed: %s\n", sc_strerror(r)); + fprintf(stderr, "sc_get_challenge() failed: %s\n", sc_strerror(r)); sc_test_cleanup(); return 1; } c++; if (c == 50) { unsigned long long foo, foo2; + gettimeofday(&tv2, NULL); foo = tv2.tv_sec * 1000 + tv2.tv_usec / 1000; foo2 = tv1.tv_sec * 1000 + tv1.tv_usec / 1000; - printf("Time per one: %lld ms\n", (foo - foo2)/50); + printf("Time per one: %lld ms\n", (foo - foo2) / 50); printf("Frequencies:\n"); for (i = 0; i < 39; i++) { - printf("%3d: %-5d", i+1, freq[i]); - if (((i+1) % 10) == 0) + printf("%3d: %-5d", i + 1, freq[i]); + if (((i + 1) % 10) == 0) printf("\n"); } printf("\n"); diff --git a/src/tests/p15dump.c b/src/tests/p15dump.c index af281d9f..e6e61306 100644 --- a/src/tests/p15dump.c +++ b/src/tests/p15dump.c @@ -1,22 +1,21 @@ /* Copyright (C) 2001 Juha Yrjölä * All rights reserved. * - * PKCS#15 PIN code test + * PKCS#15 objects test */ -#include "sc-test.h" -#include "opensc.h" -#include "opensc-pkcs15.h" #include #include +#include +#include +#include "sc-test.h" struct sc_pkcs15_card *p15card; -static int -dump_objects(const char *what, int type) +static int dump_objects(const char *what, int type) { - struct sc_pkcs15_object **objs; - int count, i; + struct sc_pkcs15_object **objs; + int count, i; printf("\nEnumerating %s... ", what); fflush(stdout); @@ -47,11 +46,10 @@ dump_objects(const char *what, int type) } free(objs); sc_unlock(card); - return (count < 0)? 1 : 0; + return (count < 0) ? 1 : 0; } -int -main(int argc, char *argv[]) +int main(int argc, char *argv[]) { int i; @@ -75,6 +73,7 @@ main(int argc, char *argv[]) dump_objects("RSA public keys", SC_PKCS15_TYPE_PUBKEY_RSA); dump_objects("X.509 certificates", SC_PKCS15_TYPE_CERT_X509); + sc_pkcs15_unbind(p15card); sc_test_cleanup(); return 0; } diff --git a/src/tests/pintest.c b/src/tests/pintest.c index 4cfe15cd..d8f72ab9 100644 --- a/src/tests/pintest.c +++ b/src/tests/pintest.c @@ -4,13 +4,13 @@ * PKCS#15 PIN code test */ -#include "sc-test.h" -#include "opensc.h" -#include "opensc-pkcs15.h" #include #include #include #include +#include +#include +#include "sc-test.h" struct sc_pkcs15_card *p15card; @@ -38,18 +38,17 @@ int enum_pins(struct sc_pkcs15_object ***ret) return n; } - int ask_and_verify_pin(struct sc_pkcs15_object *obj) { struct sc_pkcs15_pin_info *pin; int i = 0; - char prompt[80]; - u8 *pass; + char prompt[80]; + u8 *pass; pin = (struct sc_pkcs15_pin_info *) obj->data; while (1) { sprintf(prompt, "Please enter PIN code [%s]: ", obj->label); - pass = (u8 *) getpass(prompt); + pass = (u8 *) getpass(prompt); if (strlen((char *) pass) == 0) { printf("Not verifying PIN code.\n"); @@ -62,20 +61,20 @@ int ask_and_verify_pin(struct sc_pkcs15_object *obj) break; } - sc_lock(card); - i = sc_pkcs15_verify_pin(p15card, pin, pass, strlen((char *) pass)); - sc_unlock(card); - if (i) { - if (i == SC_ERROR_PIN_CODE_INCORRECT) - fprintf(stderr, - "Incorrect PIN code (%d tries left)\n", - pin->tries_left); - else - fprintf(stderr, - "PIN verifying failed: %s\n", - sc_strerror(i)); - return 1; - } else + sc_lock(card); + i = sc_pkcs15_verify_pin(p15card, pin, pass, strlen((char *) pass)); + sc_unlock(card); + if (i) { + if (i == SC_ERROR_PIN_CODE_INCORRECT) + fprintf(stderr, + "Incorrect PIN code (%d tries left)\n", + pin->tries_left); + else + fprintf(stderr, + "PIN verifying failed: %s\n", + sc_strerror(i)); + return 1; + } else printf("PIN code correct.\n"); return 0; @@ -83,8 +82,8 @@ int ask_and_verify_pin(struct sc_pkcs15_object *obj) int main(int argc, char *argv[]) { - struct sc_pkcs15_object **objs; - int i, count; + struct sc_pkcs15_object **objs; + int i, count; i = sc_test_init(&argc, argv); if (i < 0) @@ -96,6 +95,7 @@ int main(int argc, char *argv[]) sc_unlock(card); if (i) { fprintf(stderr, "failed: %s\n", sc_strerror(i)); + sc_test_cleanup(); return 1; } printf("found.\n"); @@ -103,11 +103,15 @@ int main(int argc, char *argv[]) sc_lock(card); count = enum_pins(&objs); sc_unlock(card); - if (count < 0) + if (count < 0) { + sc_pkcs15_unbind(p15card); + sc_test_cleanup(); return 1; + } for (i = 0; i < count; i++) { ask_and_verify_pin(objs[i]); } + sc_pkcs15_unbind(p15card); sc_test_cleanup(); return 0; } diff --git a/src/tests/print.c b/src/tests/print.c index 1794a68a..4fccae1c 100644 --- a/src/tests/print.c +++ b/src/tests/print.c @@ -4,18 +4,18 @@ * PKCS#15 PIN code test */ -#include "sc-test.h" -#include "opensc.h" -#include "opensc-pkcs15.h" #include #include +#include +#include +#include "sc-test.h" struct sc_pkcs15_card *p15card; -static void -print_pin(const struct sc_pkcs15_object *obj) +static void print_pin(const struct sc_pkcs15_object *obj) { - const char *pin_flags[] = { + const char *pin_flags[] = + { "case-sensitive", "local", "change-disabled", "unblock-disabled", "initialized", "needs-padding", "unblockingPin", "soPin", "disable_allowed", @@ -23,7 +23,7 @@ print_pin(const struct sc_pkcs15_object *obj) "exchangeRefData" }; struct sc_pkcs15_pin_info *pin; - const int pf_count = sizeof(pin_flags)/sizeof(pin_flags[0]); + const int pf_count = sizeof(pin_flags) / sizeof(pin_flags[0]); int i; char *p; @@ -49,37 +49,38 @@ print_pin(const struct sc_pkcs15_object *obj) printf("\n"); } -static void -print_prkey_rsa(const struct sc_pkcs15_object *obj) +static void print_prkey_rsa(const struct sc_pkcs15_object *obj) { int i; - const char *usages[] = { + const char *usages[] = + { "encrypt", "decrypt", "sign", "signRecover", "wrap", "unwrap", "verify", "verifyRecover", "derive", "nonRepudiation" }; - const int usage_count = sizeof(usages)/sizeof(usages[0]); - const char *access_flags[] = { + const int usage_count = sizeof(usages) / sizeof(usages[0]); + const char *access_flags[] = + { "sensitive", "extract", "alwaysSensitive", "neverExtract", "local" }; - const int af_count = sizeof(access_flags)/sizeof(access_flags[0]); + const int af_count = sizeof(access_flags) / sizeof(access_flags[0]); struct sc_pkcs15_prkey_info *prkey; prkey = (struct sc_pkcs15_prkey_info *) obj->data; printf("\tUsage : [0x%X]", prkey->usage); - for (i = 0; i < usage_count; i++) - if (prkey->usage & (1 << i)) { - printf(", %s", usages[i]); - } + for (i = 0; i < usage_count; i++) + if (prkey->usage & (1 << i)) { + printf(", %s", usages[i]); + } printf("\n"); printf("\tAccess Flags: [0x%X]", prkey->access_flags); - for (i = 0; i < af_count; i++) - if (prkey->access_flags & (1 << i)) { - printf(", %s", access_flags[i]); - } - printf("\n"); + for (i = 0; i < af_count; i++) + if (prkey->access_flags & (1 << i)) { + printf(", %s", access_flags[i]); + } + printf("\n"); printf("\tModLength : %d\n", prkey->modulus_length); printf("\tKey ref : %d\n", prkey->key_reference); printf("\tNative : %s\n", prkey->native ? "yes" : "no"); @@ -92,37 +93,38 @@ print_prkey_rsa(const struct sc_pkcs15_object *obj) printf("\n"); } -static void -print_pubkey_rsa(const struct sc_pkcs15_object *obj) +static void print_pubkey_rsa(const struct sc_pkcs15_object *obj) { int i; - const char *usages[] = { + const char *usages[] = + { "encrypt", "decrypt", "sign", "signRecover", "wrap", "unwrap", "verify", "verifyRecover", "derive", "nonRepudiation" }; - const int usage_count = sizeof(usages)/sizeof(usages[0]); - const char *access_flags[] = { + const int usage_count = sizeof(usages) / sizeof(usages[0]); + const char *access_flags[] = + { "sensitive", "extract", "alwaysSensitive", "neverExtract", "local" }; - const int af_count = sizeof(access_flags)/sizeof(access_flags[0]); + const int af_count = sizeof(access_flags) / sizeof(access_flags[0]); struct sc_pkcs15_pubkey_info *pubkey; pubkey = (struct sc_pkcs15_pubkey_info *) obj->data; printf("\tUsage : [0x%X]", pubkey->usage); - for (i = 0; i < usage_count; i++) - if (pubkey->usage & (1 << i)) { - printf(", %s", usages[i]); - } + for (i = 0; i < usage_count; i++) + if (pubkey->usage & (1 << i)) { + printf(", %s", usages[i]); + } printf("\n"); printf("\tAccess Flags: [0x%X]", pubkey->access_flags); - for (i = 0; i < af_count; i++) - if (pubkey->access_flags & (1 << i)) { - printf(", %s", access_flags[i]); - } - printf("\n"); + for (i = 0; i < af_count; i++) + if (pubkey->access_flags & (1 << i)) { + printf(", %s", access_flags[i]); + } + printf("\n"); printf("\tModLength : %d\n", pubkey->modulus_length); printf("\tKey ref : %d\n", pubkey->key_reference); printf("\tNative : %s\n", pubkey->native ? "yes" : "no"); @@ -135,8 +137,7 @@ print_pubkey_rsa(const struct sc_pkcs15_object *obj) printf("\n"); } -static void -print_cert_x509(const struct sc_pkcs15_object *obj) +static void print_cert_x509(const struct sc_pkcs15_object *obj) { struct sc_pkcs15_cert_info *cert; int i; @@ -155,12 +156,10 @@ print_cert_x509(const struct sc_pkcs15_object *obj) * and dump the label */ } - -void -sc_test_print_object(const struct sc_pkcs15_object *obj) +void sc_test_print_object(const struct sc_pkcs15_object *obj) { - const char *kind; - void (*printer)(const struct sc_pkcs15_object *); + const char *kind; + void (*printer) (const struct sc_pkcs15_object *); switch (obj->type) { case SC_PKCS15_TYPE_AUTH_PIN: diff --git a/src/tests/prngtest.c b/src/tests/prngtest.c index 3abc106a..2e00094d 100644 --- a/src/tests/prngtest.c +++ b/src/tests/prngtest.c @@ -4,24 +4,22 @@ * Pseudo-random number generator test program */ -#include "sc-test.h" -#include "opensc.h" -#include #include +#include #include #include +#include +#include "sc-test.h" int main(int argc, char *argv[]) { - int i, c; - int freq[256]; struct timeval tv1, tv2; + int i, c, freq[256]; u8 buf[8]; i = sc_test_init(&argc, argv); if (i < 0) return 1; - for (i = 0; i < 256; i++) freq[i] = 0; c = 0; @@ -34,7 +32,7 @@ int main(int argc, char *argv[]) gettimeofday(&tv1, NULL); i = sc_get_challenge(card, buf, 8); if (i != 0) { - fprintf(stderr, "sc_get_random() failed: %s\n", sc_strerror(i)); + fprintf(stderr, "sc_get_challenge() failed: %s\n", sc_strerror(i)); sc_test_cleanup(); return 1; } @@ -47,7 +45,7 @@ int main(int argc, char *argv[]) foo = tv2.tv_sec * 1000 + tv2.tv_usec / 1000; foo2 = tv1.tv_sec * 1000 + tv1.tv_usec / 1000; printf("\nTime to generate 64 bits of randomness: %lld ms\n", - (foo - foo2)/100); + (foo - foo2) / 100); printf("Frequencies:\n"); for (i = 0; i < 256; i++) { if (i && (i & 0x07) == 0) diff --git a/src/tests/sc-test.c b/src/tests/sc-test.c index b4eee068..2e423b81 100644 --- a/src/tests/sc-test.c +++ b/src/tests/sc-test.c @@ -55,7 +55,7 @@ int sc_test_init(int *argc, char *argv[]) printf("Connecting to card failed\n"); return i; } - printf("connected. ATR = "); + printf("connected.\nATR = "); for (i = 0; i < card->atr_len; i++) { if (i) printf(":"); diff --git a/src/tests/sc-test.h b/src/tests/sc-test.h index 63bd34be..071f5956 100644 --- a/src/tests/sc-test.h +++ b/src/tests/sc-test.h @@ -1,5 +1,5 @@ - #ifndef _SC_TEST_H +#define _SC_TEST_H extern struct sc_context *ctx; extern struct sc_card *card;