Patchwork D7210: fsmonitor: handle unicode keys in tuples

login
register
mail settings
Submitter phabricator
Date Nov. 2, 2019, 10:04 p.m.
Message ID <differential-rev-PHID-DREV-v3ruihtib6mgxdql3iez-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/42694/
State Superseded
Headers show

Comments

phabricator - Nov. 2, 2019, 10:04 p.m.
indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  In Python 3, keys in the bset tuple are str, not bytes.
  PyBytes_AsString() would return NULL. But we weren't
  checking the return value and this would lead to a segfault.
  
  This commit makes the key to char* conversion dependent on
  the Python version. The assumption here is that keys are
  always str. I believe this to be true.

REPOSITORY
  rHG Mercurial

BRANCH
  stable

REVISION DETAIL
  https://phab.mercurial-scm.org/D7210

AFFECTED FILES
  hgext/fsmonitor/pywatchman/bser.c

CHANGE DETAILS




To: indygreg, #hg-reviewers
Cc: mercurial-devel
phabricator - Nov. 4, 2019, 4:22 p.m.
durin42 added inline comments.

INLINE COMMENTS

> bser.c:182
> +#else
> +      PyObject* utf = PyUnicode_AsEncodedString(sval, "utf-8", "ignore");
> +      if (utf == NULL) {

By reading this code, I believe `sval` should have been `key`, so I've fixed that in flight.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7210/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7210

To: indygreg, #hg-reviewers
Cc: durin42, mercurial-devel

Patch

diff --git a/hgext/fsmonitor/pywatchman/bser.c b/hgext/fsmonitor/pywatchman/bser.c
--- a/hgext/fsmonitor/pywatchman/bser.c
+++ b/hgext/fsmonitor/pywatchman/bser.c
@@ -175,7 +175,15 @@ 
     const char* item_name = NULL;
     PyObject* key = PyTuple_GET_ITEM(obj->keys, i);
 
+    // tuple keys should always be str.
+#if PY_MAJOR_VERSION >= 3
+    item_name = PyUnicode_AsUTF8(key);
+#else
     item_name = PyBytes_AsString(key);
+#endif
+    if (item_name == NULL) {
+      goto bail;
+    }
     if (!strcmp(item_name, namestr)) {
       ret = PySequence_GetItem(obj->values, i);
       goto bail;