diff --git a/files-reg.c b/files-reg.c index 24e3be23f..70d2c1dd3 100644 --- a/files-reg.c +++ b/files-reg.c @@ -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, ®_desc_ops); diff --git a/include/util.h b/include/util.h index 669558cf9..2ee10ac47 100644 --- a/include/util.h +++ b/include/util.h @@ -235,6 +235,8 @@ int do_open_proc(pid_t pid, int flags, const char *fmt, ...); ___p; \ }) +#include + #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_ */