Changeset 3526

Show
Ignore:
Timestamp:
05/26/08 12:46:16 (6 months ago)
Author:
alonbl
Message:

opensc-explorer double free and cleanups

$ opensc-explorer
OpenSC Explorer version 0.11.4-svn
OpenSC [3F00]> cat
only working EFs may be read
OpenSC [3F00]> cat
only working EFs may be read
opensc-explorer: sc.c:492: sc_file_free: Assertion `sc_file_valid(file)' failed.
Aborted

$ opensc-explorer
OpenSC Explorer version 0.11.4-svn
OpenSC [3F00]> cd ff00
OpenSC [3F00/FF00]> cat
only working EFs may be read
OpenSC [3F00/FF00]> cd ..
opensc-explorer: sc.c:492: sc_file_free: Assertion `sc_file_valid(file)' failed.
Aborted

By Aktiv Co. Aleksey Samsonov
And some more Cleanups

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/tools/opensc-explorer.c

    r3510 r3526  
    352352static int do_cat(int argc, char **argv) 
    353353{ 
    354         int r, err = 0; 
     354        int r, err = 1; 
    355355        sc_path_t path; 
    356         sc_file_t *file; 
     356        sc_file_t *file = NULL; 
    357357        int not_current = 1; 
    358358 
     
    370370                if (r) { 
    371371                        check_ret(r, SC_AC_OP_SELECT, "unable to select file", current_file); 
    372                         return -1; 
     372                        goto err; 
    373373                } 
    374374        } 
    375375        if (file->type != SC_FILE_TYPE_WORKING_EF) { 
    376376                printf("only working EFs may be read\n"); 
    377                 sc_file_free(file); 
    378                 return -1; 
     377                goto err; 
    379378        } 
    380379        if (file->ef_structure == SC_FILE_EF_TRANSPARENT) 
     
    382381        else 
    383382                read_and_print_record_file(file); 
     383         
     384        err = 0; 
     385 
     386err: 
    384387        if (not_current) { 
    385                 sc_file_free(file); 
     388                if (file != NULL) { 
     389                        sc_file_free(file); 
     390                } 
    386391                r = sc_select_file(card, &current_path, NULL); 
    387392                if (r) { 
     
    390395                } 
    391396        } 
    392    return -err; 
     397 
     398        return -err; 
    393399usage: 
    394400        puts("Usage: cat [file_id]"); 
     
    833839{ 
    834840        u8 buf[256]; 
    835         int r, err = 0; 
     841        int r, err = 1; 
    836842        size_t count = 0; 
    837843        unsigned int idx = 0; 
    838844        sc_path_t path; 
    839         sc_file_t *file; 
     845        sc_file_t *file = NULL; 
    840846        char fbuf[256], *filename; 
    841847        FILE *outf = NULL; 
     
    860866        if (outf == NULL) { 
    861867                perror(filename); 
    862                 return -1; 
     868                goto err; 
    863869        } 
    864870        r = sc_select_file(card, &path, &file); 
    865871        if (r) { 
    866                 fclose(outf); 
    867872                check_ret(r, SC_AC_OP_SELECT, "unable to select file", current_file); 
    868                 return -1; 
     873                goto err; 
    869874        } 
    870875        if (file->type != SC_FILE_TYPE_WORKING_EF) { 
    871                 fclose(outf); 
    872876                printf("only working EFs may be read\n"); 
    873                 sc_file_free(file); 
    874                 return -1; 
     877                goto err; 
    875878        } 
    876879        count = file->size; 
     
    881884                if (r < 0) { 
    882885                        check_ret(r, SC_AC_OP_READ, "read failed", file); 
    883                         err = 1; 
    884886                        goto err; 
    885887                } 
    886888                if ((r != c) && !(card->caps & SC_CARD_CAP_NO_FCI)) { 
    887889                        printf("expecting %d, got only %d bytes.\n", c, r); 
    888                         err = 1; 
    889890                        goto err; 
    890891                } 
     
    897898        printf("Total of %d bytes read from %s and saved to %s.\n", 
    898899               idx, argv[0], filename); 
     900         
     901        err = 0; 
    899902err: 
    900         sc_file_free(file); 
     903        if (file) 
     904                sc_file_free(file); 
     905        if (outf) 
     906                fclose(outf); 
    901907        r = sc_select_file(card, &current_path, NULL); 
    902908        if (r) { 
     
    904910                die(1); 
    905911        } 
    906         if (outf) 
    907                 fclose(outf); 
    908912        return -err; 
    909913usage: 
     
    994998        printf("Total of %d bytes written to %04X at %i offset.\n",  
    995999               r, file->id, offs); 
     1000 
    9961001        err = 0; 
     1002 
    9971003err: 
    9981004        sc_file_free(file); 
     
    10631069               i, rec, offs); 
    10641070        err = 0; 
     1071 
    10651072err: 
    10661073        sc_file_free(file); 
     
    10811088{ 
    10821089        u8 buf[256]; 
    1083         int r, err = 0; 
     1090        int r, err = 1; 
    10841091        size_t count = 0; 
    10851092        unsigned int idx = 0; 
    10861093        sc_path_t path; 
    1087         sc_file_t *file; 
     1094        sc_file_t *file = NULL; 
    10881095        const char *filename; 
    10891096        FILE *outf = NULL; 
     
    11021109        if (outf == NULL) { 
    11031110                perror(filename); 
    1104                 return -1; 
     1111                goto err; 
    11051112        } 
    11061113        r = sc_select_file(card, &path, &file); 
    11071114        if (r) { 
    11081115                check_ret(r, SC_AC_OP_SELECT, "unable to select file", current_file); 
    1109                 fclose(outf); 
    1110                 return -1; 
     1116                goto err; 
    11111117        } 
    11121118        count = file->size; 
     
    11171123                if (r < 0) { 
    11181124                        perror("fread"); 
    1119                         err = 1; 
    11201125                        goto err; 
    11211126                } 
     
    11251130                if (r < 0) { 
    11261131                        check_ret(r, SC_AC_OP_READ, "update failed", file); 
    1127                         err = 1; 
    11281132                        goto err; 
    11291133                } 
    11301134                if (r != c) { 
    11311135                        printf("expecting %d, wrote only %d bytes.\n", c, r); 
    1132                         err = 1; 
    11331136                        goto err; 
    11341137                } 
     
    11371140        } 
    11381141        printf("Total of %d bytes written.\n", idx); 
     1142 
     1143        err = 0; 
     1144 
    11391145err: 
    1140         sc_file_free(file); 
     1146 
     1147        if (file) 
     1148                sc_file_free(file); 
     1149        if (outf) 
     1150                fclose(outf); 
    11411151        r = sc_select_file(card, &current_path, NULL); 
    11421152        if (r) { 
     
    11441154                die(1); 
    11451155        } 
    1146         if (outf) 
    1147                 fclose(outf); 
    11481156        return -err; 
    11491157usage: 
     
    15131521static int do_asn1(int argc, char **argv) 
    15141522{ 
    1515         int r; 
     1523        int r, err = 1; 
    15161524        sc_path_t path; 
    1517         sc_file_t *file; 
     1525        sc_file_t *file = NULL; 
    15181526        int not_current = 1; 
    15191527        size_t len; 
    1520         char *buf; 
     1528        unsigned char *buf = NULL; 
    15211529 
    15221530        if (argc > 1) { 
     
    15251533        } 
    15261534 
    1527         // select file 
     1535        /* select file */ 
    15281536        if (argc) { 
    15291537                if (arg_to_path(argv[0], &path, 1) != 0) { 
     
    15341542                if (r) { 
    15351543                        check_ret(r, SC_AC_OP_SELECT, "unable to select file", current_file); 
    1536                         return -1; 
     1544                        goto err; 
    15371545                } 
    15381546        } else { 
     
    15431551        if (file->type != SC_FILE_TYPE_WORKING_EF) { 
    15441552                printf("only working EFs may be read\n"); 
    1545                 sc_file_free(file); 
    1546                 return -1; 
    1547         } 
    1548  
    1549         // read 
     1553                goto err; 
     1554        } 
     1555 
     1556        /* read */ 
    15501557        if (file->ef_structure != SC_FILE_EF_TRANSPARENT) { 
    15511558                printf("only transparent file type is supported at the moment\n"); 
    1552                 sc_file_free(file); 
    1553                 return -1; 
     1559                goto err; 
    15541560        } 
    15551561        len = file->size; 
    15561562        buf = calloc(1, len); 
    1557         if (!buf) die(1); 
     1563        if (!buf) { 
     1564                goto err; 
     1565        } 
    15581566        r = sc_read_binary(card, 0, buf, len, 0); 
    15591567        if (r < 0) { 
    15601568                check_ret(r, SC_AC_OP_READ, "read failed", file); 
     1569                goto err; 
     1570        } 
     1571        if ((size_t)r != len) { 
     1572                printf("expecting %d, got only %d bytes.\n", len, r); 
     1573                goto err; 
     1574        } 
     1575 
     1576        /* asn1 dump */ 
     1577        sc_asn1_print_tags(buf, len); 
     1578 
     1579        err = 0; 
     1580err: 
     1581        if (buf) 
    15611582                free(buf); 
    1562                 return -1; 
    1563         } 
    1564         if (r != len) { 
    1565                 printf("expecting %d, got only %d bytes.\n", len, r); 
    1566                 free(buf); 
    1567                 return -1; 
    1568         } 
    1569  
    1570         // asn1 dump 
    1571         sc_asn1_print_tags(buf, len); 
    1572  
    15731583        if (not_current) { 
    1574                 sc_file_free(file); 
     1584                if (file) 
     1585                        sc_file_free(file); 
    15751586                r = sc_select_file(card, &current_path, NULL); 
    15761587                if (r) { 
     
    15791590                } 
    15801591        } 
    1581  
    1582         return 0; 
     1592        return -err; 
    15831593} 
    15841594