memory leak

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

memory leak

Imanishi Masayuki
Hello.

I am writing PKCS15 application code that is based on OpenSC 0.9.6.
I am using Axalto(Schlumberger) E-gate.

I found memory leak.
 - function openct_reader_release [libopensc/reader-openct.c]
 - function cryptoflex_init_card [pkcs15init/pkcs15-cflex.c]

I attached a patch for this memory leak.
Please check this patch.

Imanishi.

diff -cr src/libopensc/reader-openct.c src/libopensc/reader-openct.c
*** src/libopensc/reader-openct.c 2005-04-14 18:02:16.000000000 +0900
--- src/libopensc/reader-openct.c 2005-07-14 11:43:07.000000000 +0900
***************
*** 167,172 ****
--- 167,173 ----
  openct_reader_release(struct sc_reader *reader)
  {
   struct driver_data *data = (struct driver_data *) reader->drv_data;
+  int i;
 
   SC_FUNC_CALLED(reader->ctx, 1);
   if (data) {
***************
*** 175,180 ****
--- 176,186 ----
    memset(data, 0, sizeof(*data));
    reader->drv_data = NULL;
    free(data);
+
+   for (i = 0; i < SC_MAX_SLOTS; i++) {
+       if(reader->slot[i].drv_data)
+    free(reader->slot[i].drv_data);
+   }
   }
   
   return SC_NO_ERROR;

diff -cr src/pkcs15init/pkcs15-cflex.c src/pkcs15init/pkcs15-cflex.c
*** src/pkcs15init/pkcs15-cflex.c 2005-04-14 18:02:02.000000000 +0900
--- src/pkcs15init/pkcs15-cflex.c 2005-07-14 15:25:42.000000000 +0900
***************
*** 148,153 ****
--- 148,154 ----
   if (len == 0)
    return 0;
 
+  sc_file_free(file);
   if ((r = sc_bin_to_hex(buf, len, serial, sizeof(serial), '\0')) < 0)
    return r;
   sc_pkcs15init_set_serial(profile, serial);
_______________________________________________
opensc-devel mailing list
[hidden email]
http://www.opensc.org/cgi-bin/mailman/listinfo/opensc-devel

reader-openct.c.diff (875 bytes) Download Attachment
pkcs15-cflex.c.diff (519 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: memory leak

Nils Larsch
Imanishi Masayuki wrote:

> Hello.
>
> I am writing PKCS15 application code that is based on OpenSC 0.9.6.
> I am using Axalto(Schlumberger) E-gate.
>
> I found memory leak.
>  - function openct_reader_release [libopensc/reader-openct.c]
>  - function cryptoflex_init_card [pkcs15init/pkcs15-cflex.c]
>
> I attached a patch for this memory leak.
> Please check this patch.
>
> Imanishi.
>
> diff -cr src/libopensc/reader-openct.c src/libopensc/reader-openct.c
> *** src/libopensc/reader-openct.c 2005-04-14 18:02:16.000000000 +0900
> --- src/libopensc/reader-openct.c 2005-07-14 11:43:07.000000000 +0900
> ***************
> *** 167,172 ****
> --- 167,173 ----
>   openct_reader_release(struct sc_reader *reader)
>   {
>    struct driver_data *data = (struct driver_data *) reader->drv_data;
> +  int i;
>  
>    SC_FUNC_CALLED(reader->ctx, 1);
>    if (data) {
> ***************
> *** 175,180 ****
> --- 176,186 ----
>     memset(data, 0, sizeof(*data));
>     reader->drv_data = NULL;
>     free(data);
> +
> +   for (i = 0; i < SC_MAX_SLOTS; i++) {
> +       if(reader->slot[i].drv_data)
> +    free(reader->slot[i].drv_data);
> +   }
>    }
>    
>    return SC_NO_ERROR;

looks reasonable => committed

> diff -cr src/pkcs15init/pkcs15-cflex.c src/pkcs15init/pkcs15-cflex.c
> *** src/pkcs15init/pkcs15-cflex.c 2005-04-14 18:02:02.000000000 +0900
> --- src/pkcs15init/pkcs15-cflex.c 2005-07-14 15:25:42.000000000 +0900
> ***************
> *** 148,153 ****
> --- 148,154 ----
>    if (len == 0)
>     return 0;
>  
> +  sc_file_free(file);
>    if ((r = sc_bin_to_hex(buf, len, serial, sizeof(serial), '\0')) < 0)
>     return r;
>    sc_pkcs15init_set_serial(profile, serial);

I've committed a slightly different patch as in case of an error
(if sc_read_binary fails) we would still have a memory leak.

Please test a recent snapshot.

Thanks for the report,
Nils
_______________________________________________
opensc-devel mailing list
[hidden email]
http://www.opensc.org/cgi-bin/mailman/listinfo/opensc-devel