Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fsevents segfault #763

Merged
merged 9 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
Changelog
---------

2.0.1
~~~~~

2021-02-xx • `full history <https://github.com/gorakhargosh/watchdog/compare/v2.0.0...master>`__

- [mac] Fix a segmentation fault when dealing with unicode paths (`#763 <https://github.com/gorakhargosh/watchdog/pull/763>`_)
- Thanks to our beloved contributors: @SamSchott


2.0.0
~~~~~

Expand Down
27 changes: 19 additions & 8 deletions src/watchdog_fsevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,26 @@ static void watchdog_pycapsule_destructor(PyObject *ptr)
*/
PyObject * CFString_AsPyUnicode(CFStringRef cf_string_ref)
{
CFIndex cf_string_length;
char *c_string_buff = NULL;
const char *c_string_ptr;

if (G_IS_NULL(cf_string_ref)) {
return PyUnicode_FromString("");
}

PyObject *py_string;

c_string_ptr = CFStringGetCStringPtr(cf_string_ref, 0);
const char *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_size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
char *buffer = (char *)malloc(max_size);
if (CFStringGetCString(cf_string_ref, buffer, max_size, kCFStringEncodingUTF8)) {
py_string = PyUnicode_FromString(buffer);
}
else {
py_string = PyUnicode_FromString("");
}
free(buffer);
} else {
py_string = PyUnicode_FromString(c_string_ptr);
}
Expand Down Expand Up @@ -389,8 +398,10 @@ watchdog_FSEventStreamCallback(ConstFSEventStreamRef stream_ref,
py_event_inodes = PyList_New(num_events);
py_event_flags = PyList_New(num_events);
py_event_ids = PyList_New(num_events);
if (G_NOT(event_path_info_array_ref && py_event_flags && py_event_ids))
if (G_NOT(py_event_paths && py_event_inodes && py_event_flags && py_event_ids))
{
Py_XDECREF(py_event_paths);
Py_XDECREF(py_event_inodes);
Py_XDECREF(py_event_ids);
Py_XDECREF(py_event_flags);
return /*NULL*/;
Expand Down
19 changes: 18 additions & 1 deletion tests/test_fsevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def teardown_function(function):
def start_watching(path=None, use_full_emitter=False):
global emitter
path = p("") if path is None else path
emitter = FSEventsEmitter(event_queue, ObservedWatch(path, recursive=True))
emitter = FSEventsEmitter(event_queue, ObservedWatch(path, recursive=True), suppress_history=True)
emitter.start()


Expand Down Expand Up @@ -127,6 +127,23 @@ def on_thread_stop(self):
observer.unschedule(w)


def test_converting_cfstring_to_pyunicode():
"""See https://github.com/gorakhargosh/watchdog/issues/762
"""

tmpdir = p()
start_watching(tmpdir)

dirname = "TéstClass"

try:
mkdir(p(dirname))
event, _ = event_queue.get()
assert event.src_path.endswith(dirname)
finally:
emitter.stop()


def test_watchdog_recursive():
""" See https://github.com/gorakhargosh/watchdog/issues/706
"""
Expand Down