watchdog: SEGFAULT on macOS with the latest version of watchdog

Unfortunately, this will be tough to describe, since I don’t have a mac and only my CI is complaining.

I’m using watchdog as a submodule and today tried to update it. One specific test consistently makes pytest segfault.

This is the log from a SEGFAULTING run: https://dev.azure.com/borisstaletic/3ce92110-caa5-4c49-b8c3-44a433da676b/_apis/build/builds/1535/logs/64 The way I’m using watchdog is by talking to, in this case, jdt.ls. When I open this file it sends… these patterns:

[
  {
    "globPattern": "**/*.java"
  },
  {
    "globPattern": "**/.project"
  },
  {
    "globPattern": "**/.classpath"
  },
  {
    "globPattern": "**/.settings/*.prefs"
  },
  {
    "globPattern": "**/src/**"
  },
  {
    "globPattern": "**/*.gradle"
  },
  {
    "globPattern": "**/gradle.properties"
  },
  {
    "globPattern": "**/pom.xml"
  },
  {
    "globPattern": "/home/bstaletic/work/ycmd/ycmd/tests/java/testdata/simple_eclipse_project",
    "kind": 4
  }
]

Which we then edit, like this: https://github.com/ycm-core/ycmd/blob/master/ycmd/completers/language_server/language_server_completer.py#L590-L591

And finally use that with this handler: https://github.com/ycm-core/ycmd/blob/master/ycmd/completers/language_server/language_server_completer.py#L3219

Any further information, as well as running the CI, I can gladly provide.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@puremourning, this is very helpful, thanks! It also explains why the segfault didn’t show up in our testing: the code block which you show is only called as a backup when CFStringGetCStringPtr returns NULL. I suspect that the string length is incorrectly calculated there.

Could you try applying this patch and see if it fixes the problem:

diff --git a/src/watchdog_fsevents.c b/src/watchdog_fsevents.c
index e9f4e6aced800936af2778cc046fa6382dceae13..92f3ed0c7e067bda0f362cf022d7af2f99c5f69e 100644
--- a/src/watchdog_fsevents.c
+++ b/src/watchdog_fsevents.c
@@ -293,17 +293,27 @@ static void watchdog_pycapsule_destructor(PyObject *ptr)
  */
 PyObject * CFString_AsPyUnicode(CFStringRef cf_string_ref)
 {
-    CFIndex cf_string_length;
-    char *c_string_buff = NULL;
+
+    if (G_IS_NULL(cf_string_ref)) {
+        return PyUnicode_FromString("");
+    }
+
     const char *c_string_ptr;
     PyObject *py_string;
 
-    c_string_ptr = CFStringGetCStringPtr(cf_string_ref, 0);
+    c_string_ptr = CFStringGetCStringPtr(cf_string_ref, kCFStringEncodingUTF8);
 
     if (G_IS_NULL(c_string_ptr)) {
-        cf_string_length = CFStringGetLength(cf_string_ref);
-        CFStringGetCString(cf_string_ref, c_string_buff, cf_string_length, 0);
-        py_string = PyUnicode_FromString(c_string_buff);
+        CFIndex length = CFStringGetLength(cf_string_ref);
+        CFIndex max_length = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
+        char *buffer = (char *)malloc(max_length);
+        if (CFStringGetCString(cf_string_ref, buffer, max_length, kCFStringEncodingUTF8)) {
+            py_string = PyUnicode_FromString(buffer);
+        }
+        else {
+            py_string = PyUnicode_FromString("");
+        }
+        free(buffer);
     } else {
         py_string = PyUnicode_FromString(c_string_ptr);
     }