printf("Cannot seek to start of file '%s'\n", fname);
goto err;
}
- *bufp = malloc(size);
+ *bufp = os_malloc(size);
if (!*bufp) {
printf("Not enough memory to read file '%s'\n", fname);
ret = -ENOMEM;
state->argv = argv;
/* dynamically construct the arguments to the system getopt_long */
- short_opts = malloc(sizeof(*short_opts) * num_options * 2 + 1);
- long_opts = malloc(sizeof(*long_opts) * (num_options + 1));
+ short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
+ long_opts = os_malloc(sizeof(*long_opts) * (num_options + 1));
if (!short_opts || !long_opts)
return 1;
while (node) {
next = node->next;
- free(node);
+ os_free(node);
node = next;
}
}
/* Create a buffer upfront, with typically sufficient size */
dirlen = strlen(dirname) + 2;
len = dirlen + 256;
- fname = malloc(len);
+ fname = os_malloc(len);
if (!fname) {
ret = -ENOMEM;
goto done;
ret = errno;
break;
}
- next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
+ next = os_malloc(sizeof(*node) + strlen(entry->d_name) + 1);
if (!next) {
os_dirent_free(head);
ret = -ENOMEM;
if (dirlen + strlen(entry->d_name) > len) {
len = dirlen + strlen(entry->d_name);
old_fname = fname;
- fname = realloc(fname, len);
+ fname = os_realloc(fname, len);
if (!fname) {
- free(old_fname);
- free(next);
+ os_free(old_fname);
+ os_free(next);
os_dirent_free(head);
ret = -ENOMEM;
goto done;
done:
closedir(dir);
- free(fname);
+ os_free(fname);
return ret;
}
for (argc = 0; (*argvp)[argc]; argc++)
;
- argv = malloc((argc + count + 1) * sizeof(char *));
+ argv = os_malloc((argc + count + 1) * sizeof(char *));
if (!argv) {
printf("Out of memory for %d argv\n", count);
return -ENOMEM;
os_exit(2);
err = execv(fname, argv);
- free(argv);
+ os_free(argv);
if (err) {
perror("Unable to run image");
printf("Image filename '%s'\n", fname);
return ret;
}
- /* Set up spl_image to boot from jump_to_image_no_args() */
- spl_image->arg = strdup(fname);
+ /*
+ * Set up spl_image to boot from jump_to_image_no_args(). Allocate this
+ * outsdide the RAM buffer (i.e. don't use strdup()).
+ */
+ spl_image->arg = os_malloc(strlen(fname) + 1);
if (!spl_image->arg)
- return log_msg_ret("Setup exec filename", -ENOMEM);
+ return log_msg_ret("exec", -ENOMEM);
+ strcpy(spl_image->arg, fname);
return 0;
}
/* Sort the options */
size = sizeof(*sorted_opt) * num_options;
- sorted_opt = malloc(size);
+ sorted_opt = os_malloc(size);
if (!sorted_opt) {
printf("No memory to sort options\n");
os_exit(1);
int len;
len = strlen(state->argv[0]) + strlen(fmt) + 1;
- fname = malloc(len);
+ fname = os_malloc(len);
if (!fname)
return -ENOMEM;
snprintf(fname, len, fmt, state->argv[0]);
int len;
len = strlen(state->argv[0]) + strlen(fmt) + 1;
- fname = malloc(len);
+ fname = os_malloc(len);
if (!fname)
return -ENOMEM;
strcpy(fname, state->argv[0]);
{
struct sandbox_state *state;
gd_t data;
+ int size;
int ret;
/*
* Copy argv[] so that we can pass the arguments in the original
* sequence when resetting the sandbox.
*/
- os_argv = calloc(argc + 1, sizeof(char *));
+ size = sizeof(char *) * (argc + 1);
+ os_argv = os_malloc(size);
if (!os_argv)
os_exit(1);
- memcpy(os_argv, argv, sizeof(char *) * (argc + 1));
+ memcpy(os_argv, argv, size);
memset(&data, '\0', sizeof(data));
gd = &data;
return 0;
size = used + extra_size;
- buf = malloc(size);
+ buf = os_malloc(size);
if (!buf)
return -ENOMEM;
ret = fdt_open_into(blob, buf, size);
if (ret) {
- free(buf);
+ os_free(buf);
return -EIO;
}
- free(blob);
+ os_free(blob);
state->state_fdt = buf;
return 0;
}
printf("Cannot find sandbox state file '%s'\n", fname);
return -ENOENT;
}
- state->state_fdt = malloc(size);
+ state->state_fdt = os_malloc(size);
if (!state->state_fdt) {
puts("No memory to read sandbox state\n");
return -ENOMEM;
err_read:
os_close(fd);
err_open:
- free(state->state_fdt);
+ os_free(state->state_fdt);
state->state_fdt = NULL;
return ret;
/* Create a state FDT if we don't have one */
if (!state->state_fdt) {
size = 0x4000;
- state->state_fdt = malloc(size);
+ state->state_fdt = os_malloc(size);
if (!state->state_fdt) {
puts("No memory to create FDT\n");
return -ENOMEM;
err_write:
os_close(fd);
err_create:
- free(state->state_fdt);
+ os_free(state->state_fdt);
return ret;
}
if (state->jumped_fname)
os_unlink(state->jumped_fname);
- if (state->state_fdt)
- free(state->state_fdt);
+ os_free(state->state_fdt);
+ os_free(state->ram_buf);
memset(state, '\0', sizeof(*state));
return 0;
*
* This returns the memory to the OS.
*
- * @ptr: Pointer to memory block to free
+ * @ptr: Pointer to memory block to free. If this is NULL then this
+ * function does nothing
*/
void os_free(void *ptr);