Commit 57095eb9 authored by Pavel Emelyanov's avatar Pavel Emelyanov

util: Helper for reading a string from image

Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent fc416d86
......@@ -394,8 +394,6 @@ int collect_reg_files(void)
return -1;
while (1) {
int len;
rfi = xmalloc(sizeof(*rfi));
ret = -1;
if (rfi == NULL)
......@@ -406,18 +404,11 @@ int collect_reg_files(void)
if (ret <= 0)
break;
len = rfi->rfe.len;
rfi->path = xmalloc(len + 1);
ret = -1;
if (rfi->path == NULL)
break;
ret = read_img_buf(fd, rfi->path, len);
ret = read_img_str(fd, &rfi->path, rfi->rfe.len);
if (ret < 0)
break;
rfi->remap_path = NULL;
rfi->path[len] = '\0';
pr_info("Collected [%s] ID %#x\n", rfi->path, rfi->rfe.id);
file_desc_add(&rfi->d, rfi->rfe.id, &reg_desc_ops);
......
......@@ -235,6 +235,8 @@ int do_open_proc(pid_t pid, int flags, const char *fmt, ...);
___p; \
})
#include <stdlib.h>
#define xstrdup(str) __xalloc(strdup, strlen(str) + 1, str)
#define xmalloc(size) __xalloc(malloc, size, size)
#define xzalloc(size) __xalloc(calloc, size, 1, size)
......@@ -287,4 +289,29 @@ int is_anon_link_type(int lfd, char *type);
((c) >= 'a' && (c) <= 'f') || \
((c) >= 'A' && (c) <= 'F'))
/*
* read_img_str -- same as read_img_buf, but allocates memory for
* the buffer and puts the '\0' at the end
*/
static inline int read_img_str(int fd, char **pstr, int size)
{
int ret;
char *str;
str = xmalloc(size + 1);
if (!str)
return -1;
ret = read_img_buf(fd, str, size);
if (ret < 0) {
xfree(str);
return -1;
}
str[size] = '\0';
*pstr = str;
return 0;
}
#endif /* UTIL_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment