lib: Fix file_lines_parse() to do what people expect. Much safer to use.
authorJeremy Allison <jra@samba.org>
Tue, 10 Nov 2020 21:52:01 +0000 (13:52 -0800)
committerGünther Deschner <gd@samba.org>
Fri, 13 Nov 2020 16:22:32 +0000 (16:22 +0000)
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 <jra@samba.org>
Reviewed-by: Guenther Deschner <gd@samba.org>
lib/util/samba_util.h
lib/util/util_file.c

index 5a81baa80b633b6f1ae69c830285b7ca7ccd44f9..3a60b618083f9dc6f691e172ed0da772f9694c93 100644 (file)
@@ -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
index c95d02dd76f197bd699d9dcc9c410cd235f2676a..af90e4a76215d3696827c9f9ddba2902b57cf970 100644 (file)
@@ -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);
 }