From: Jeremy Allison Date: Tue, 10 Nov 2020 21:52:01 +0000 (-0800) Subject: lib: Fix file_lines_parse() to do what people expect. Much safer to use. X-Git-Tag: samba-4.14.0rc1~622 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ae4dd2ab82eae1ddffbb00e7a753521a0879341b;p=thirdparty%2Fsamba.git lib: Fix file_lines_parse() to do what people expect. Much safer to use. Take an incoming const char * pointer and return an allocated array that must be freed. Don't expose the internal optimization of file_lines_parse_internal() breaking the passed in string into lines. Signed-off-by: Jeremy Allison Reviewed-by: Guenther Deschner --- diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index 5a81baa80b6..3a60b618083 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -394,7 +394,7 @@ load a file into memory from a fd. _PUBLIC_ char *fd_load(int fd, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx); -char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx); +char **file_lines_parse(const char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx); /** load a file into memory diff --git a/lib/util/util_file.c b/lib/util/util_file.c index c95d02dd76f..af90e4a7621 100644 --- a/lib/util/util_file.c +++ b/lib/util/util_file.c @@ -324,11 +324,20 @@ _PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX return file_lines_parse_internal(p, size, numlines, mem_ctx); } -_PUBLIC_ char **file_lines_parse(char *p, +_PUBLIC_ char **file_lines_parse(const char *p_in, size_t size, int *numlines, TALLOC_CTX *mem_ctx) { + /* + * Copy the incoming string so it can end up + * being owned by the returned pointer and + * freed when that is. + */ + char *p = talloc_strdup(mem_ctx, p_in); + if (p == NULL) { + return NULL; + } return file_lines_parse_internal(p, size, numlines, mem_ctx); }