[Webkit-unassigned] [Bug 187174] New: [Linux] Fix memory leak in WTF::forEachLine()

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Fri Jun 29 04:00:33 PDT 2018


https://bugs.webkit.org/show_bug.cgi?id=187174

            Bug ID: 187174
           Summary: [Linux] Fix memory leak in WTF::forEachLine()
           Product: WebKit
           Version: WebKit Nightly Build
          Hardware: Unspecified
                OS: Unspecified
            Status: NEW
          Severity: Normal
          Priority: P2
         Component: WebKit Gtk
          Assignee: webkit-unassigned at lists.webkit.org
          Reporter: aboya at igalia.com
                CC: bugs-noreply at webkitgtk.org

getline() allocates a buffer even when the read fails because EOF. This buffer also needs to be freed.

Also note the buffer is intended to be reused to avoid making an allocation each time a line is read, getline() will realloc() the buffer autmatically if necessary.

See `man getline()` for an example:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
   FILE *stream;
   char *line = NULL;
   size_t len = 0;
   ssize_t nread;

   if (argc != 2) {
       fprintf(stderr, "Usage: %s <file>\n", argv[0]);
       exit(EXIT_FAILURE);
   }

   stream = fopen(argv[1], "r");
   if (stream == NULL) {
       perror("fopen");
       exit(EXIT_FAILURE);
   }

   while ((nread = getline(&line, &len, stream)) != -1) {
       printf("Retrieved line of length %zu:\n", nread);
       fwrite(line, nread, 1, stdout);
   }

   free(line);
   fclose(stream);
   exit(EXIT_SUCCESS);
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-unassigned/attachments/20180629/9233d05e/attachment.html>


More information about the webkit-unassigned mailing list