[Webkit-unassigned] [Bug 210346] New: [GTK][WPE] Replace evil strtok() calls with fscanf() in MemoryPressureMonitor.cpp

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Fri Apr 10 10:29:50 PDT 2020


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

            Bug ID: 210346
           Summary: [GTK][WPE] Replace evil strtok() calls with fscanf()
                    in MemoryPressureMonitor.cpp
           Product: WebKit
           Version: WebKit Nightly Build
          Hardware: Unspecified
                OS: Linux
            Status: NEW
          Severity: Normal
          Priority: P2
         Component: WebKitGTK
          Assignee: webkit-unassigned at lists.webkit.org
          Reporter: psaavedra at igalia.com
                CC: aperez at igalia.com, bugs-noreply at webkitgtk.org
        Depends on: 209942

Related to https://bugs.webkit.org/show_bug.cgi?id=209942
See details in https://bugs.webkit.org/show_bug.cgi?id=209942#c18


> Source/WebKit/UIProcess/linux/MemoryPressureMonitor.cpp:173
>  // 1:name=systemd:/user.slice/user-1000.slice/user at 1000.service/gnome-terminal-server.service


These lines look quite suitable to be parsed with fscanf():

  while (!feof(inputFile)) {
      char name[40 + 1];
      char path[PATH_MAX + 1];
      fscanf("%*u:%40[^:]:%" STRINGIFY(PATH_MAX) "[^\n]", name, path);
      if (!strcmp(name, "name=systemd"))
          return CString(path);
  }

  // Not found
  return CString();

With the STRINGIFY() macro being the usual preprocessor trick:

  #define STRINGIFY_EXPANDED(val) #val
  #define STRINGIFY(val) STRINGIFY_EXPANDED(val)

> Source/WebKit/UIProcess/linux/MemoryPressureMonitor.cpp:217
> +    while (char* line = fgets(buffer, 128, memInfoFile)) {


Idea: instead of going line-by-line and using evil strtok(), take advantage of… fscanf() again!

   memoryTotal = memoryFree = activeFile = inactiveFile = slabReclaimable = notSet;
   while (!feof(memInfoFile)) {
       char token[50 + 1];
       size_t amount;
       if (fscanf(memInfoFile, "%50s%zukB", token, &amount) != 2)
           break;

       if (!strcmp(token, "MemTotal:"))
           memoryTotal = amount;
       else if (!strcmp(token, "MemFree:"))
           memoryFree = amount;
       else ...

      if (memoryTotal != notSet && memoryFree != notSet && activeFile != notSet && inactiveFile != notSet && slabReclaimable != notSet)
          break;
   }

Note how this avoids needing to manually split the input in tokens ourselves: instead we let fscanf() do the hard work of parsing the input and doing numeric conversions.

> Source/WebKit/UIProcess/linux/MemoryPressureMonitor.cpp:420
> +    return value;


This whole function can be implemented as:

  size_t CGroupMemoryController::getCgroupFileValue(FILE* file)
  {
      size_t value;
      return (file && fscanf(file, "%zu", &value) == 1) ? value : notSet;
  }


Referenced Bugs:

https://bugs.webkit.org/show_bug.cgi?id=209942
[Bug 209942] [GTK][WPE] Replace fopen/fclose by fopen/fseek functions in MemoryPressureMonitor
-- 
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/20200410/a3ca3fd1/attachment-0001.htm>


More information about the webkit-unassigned mailing list