Changeset 3e5c48c9 in mainline for uspace/app/bdsh/compl.c


Ignore:
Timestamp:
2011-07-13T20:44:04Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
159100a
Parents:
9be9c4d
Message:

Fix memory leak in shell completion.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/compl.c

    r9be9c4d r3e5c48c9  
    6767        /** Pointer inside list of directories */
    6868        const char **path;
     69        /** If not @c NULL, should be freed in the end. */
     70        const char **path_list;
    6971        /** Current open directory */
    7072        DIR *dir;
     
    163165                }
    164166                *cstart += rpath_sep + 1 - prefix;
    165 //              printf("cstart=%zu\n", *cstart);
    166167                free(prefix);
    167168
    168 //              printf("\ncs->prefix='%s', dirname='%s'\n", cs->prefix, dirname);
    169 
    170                 static const char *dlist2[2];
    171                 dlist2[0] = dirname;
    172                 dlist2[1] = NULL;
    173                 cs->path = dlist2;
    174                 /* XXX dirname won't be freed */
     169                cs->path_list = malloc(sizeof(char *) * 2);
     170                if (cs->path_list == NULL) {
     171                        retval = ENOMEM;
     172                        goto error;
     173                }
     174                cs->path_list[0] = dirname;
     175                cs->path_list[1] = NULL;
     176                cs->path = cs->path_list;
     177
    175178        } else if (cs->is_command) {
    176179                /* Command without path */
     
    193196        /* Error cleanup */
    194197
     198        if (cs != NULL && cs->path_list != NULL) {
     199                size_t i = 0;
     200                while (cs->path_list[i] != NULL) {
     201                        free(cs->path_list[i]);
     202                        ++i;
     203                }
     204                free(cs->path_list);
     205        }
     206
    195207        if (cs != NULL && cs->prefix != NULL)
    196208                free(cs->prefix);
     
    271283
    272284                        /* If it was the last one, we are done */
    273                         if (cs->dir == NULL) {
    274                                 cs->path = NULL;
     285                        if (cs->dir == NULL)
    275286                                break;
    276                         }
    277287
    278288                        /* Read next directory entry */
     
    322332        compl_t *cs = (compl_t *) state;
    323333
     334        if (cs->path_list != NULL) {
     335                size_t i = 0;
     336                while (cs->path_list[i] != NULL) {
     337                        free(cs->path_list[i]);
     338                        ++i;
     339                }
     340                free(cs->path_list);
     341        }
     342
    324343        if (cs->last_compl != NULL)
    325344                free(cs->last_compl);
Note: See TracChangeset for help on using the changeset viewer.