Changeset 587867a in mainline for uspace/app/bdsh/exec.c


Ignore:
Timestamp:
2019-03-16T13:01:30Z (5 years ago)
Author:
Matthieu Riolo <matthieu.riolo@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
aeeaf0f
Parents:
1c481ee
Message:

improve error handling

The code did not test if malloc returned NULL.
This meant that the code potentially could break without
that the user was informed.

File:
1 edited

Legend:

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

    r1c481ee r587867a  
    4747#include "errors.h"
    4848
    49 static char *find_command(char *);
     49static errno_t find_command(char *, char **);
    5050static int try_access(const char *);
    5151
     
    6565}
    6666
    67 /** Returns the full path of "cmd" if cmd is found
     67/** Returns EOK if no internal failure or else ENOMEM
    6868 *
    69  * else just hand back cmd as it was presented
     69 * When the parameter `cmd` can be found then the absolute path will be set in `found`.
     70 * Or else `found` will be NULL.
     71 * `found` will be newly allocated and must be freed by the caller
    7072 */
    71 static char *find_command(char *cmd)
     73static errno_t find_command(char *cmd, char **found)
    7274{
    7375        /* The user has specified a full or relative path, just give it back. */
    7476        if (is_path(cmd)) {
    7577                if (-1 != try_access(cmd)) {
    76                         return str_dup(cmd);
     78                        *found = str_dup(cmd);
     79                        return EOK;
     80                }
     81
     82                *found = NULL;
     83                return EOK;
     84        }
     85
     86        *found = (char *)malloc(PATH_MAX);
     87        if (*found == NULL) {
     88                return ENOMEM;
     89        }
     90
     91        /* We now have n places to look for the command */
     92        size_t i;
     93        size_t cmd_length = str_length(cmd);
     94        for (i = 0; search_dir[i] != NULL; i++) {
     95                if (str_length(search_dir[i]) + cmd_length + 2 > PATH_MAX) {
     96                        free(*found);
     97                        return ENOMEM;
     98                }
     99
     100                memset(*found, 0, PATH_MAX);
     101                snprintf(*found, PATH_MAX, "%s/%s", search_dir[i], cmd);
     102                if (-1 != try_access(*found)) {
     103                        return EOK;
    77104                }
    78105        }
     106        free(*found);
     107        *found = NULL;
    79108
    80         char *found = (char *)malloc(PATH_MAX);
    81         /* We now have n places to look for the command */
    82         size_t i;
    83         for (i = 0; search_dir[i] != NULL; i++) {
    84                 memset(found, 0, PATH_MAX);
    85                 snprintf(found, PATH_MAX, "%s/%s", search_dir[i], cmd);
    86                 if (-1 != try_access(found)) {
    87                         return (char *) found;
    88                 }
    89         }
    90         free(found);
    91 
    92         /* We didn't find it, just give it back as-is. */
    93         return str_dup(cmd);
     109        /* We didn't find it, return NULL */
     110        return EOK;
    94111}
    95112
     
    105122        FILE *files[3];
    106123
    107         tmp = find_command(cmd);
     124        rc = find_command(cmd, &tmp);
     125        if (rc != EOK) {
     126                cli_error(CL_ENOMEM, "%s: failure executing find_command()", progname);
     127                return 1;
     128        }
     129
     130        if (tmp == NULL) {
     131                cli_error(CL_EEXEC, "%s: Command not found '%s'", progname, cmd);
     132                return 1;
     133        }
    108134
    109135        files[0] = io->stdin;
Note: See TracChangeset for help on using the changeset viewer.