lol

python3Packages.libxml2: Patch to work around python3 + utf-8 itstool crash

1. Gnumeric has unbalanced XML tags in its doc translations.
2. itstool's XML error handler tries to print this error with context.
3. libxml2's context snipper treats the data as bytes, not UTF-8.
4. python3Packages.libxml2 casts the context to a UTF-8 Python string.
5. itstool dereferences a null pointer.

This patch intervenes at #4.

In https://bugzilla.gnome.org/show_bug.cgi?id=789714#c4 , upstream
suggests that intervening at #3 would be better -- that each of the four
copies of xmlParserPrintFileContextInternal() have four additional UTF-8
problems, one of which is that the caret indicator ought to count
"unicode characters" not bytes. But to position a caret correctly, a
character count is not sufficient -- this would need to use icu's BiDi
logic (with fallback to doing something wrong when libxml2 is configured
not to use icu) -- which makes a 'correct' fix a much larger project
than this simple band-aid.

Chuck c0cecd0e 0c999c75

+44
+14
pkgs/development/libraries/libxml2/default.nix
··· 14 14 url = "http://xmlsoft.org/sources/${pname}-${version}.tar.gz"; 15 15 sha256 = "0wd881jzvqayx0ihzba29jl80k06xj9ywp16kxacdqs3064p1ywl"; 16 16 }; 17 + patches = [ 18 + # Upstream bugs: 19 + # https://bugzilla.gnome.org/show_bug.cgi?id=789714 20 + # https://gitlab.gnome.org/GNOME/libxml2/issues/64 21 + # Patch from https://bugzilla.opensuse.org/show_bug.cgi?id=1065270 , 22 + # but only the UTF-8 part. 23 + # Can also be mitigated by fixing malformed XML inputs, such as in 24 + # https://gitlab.gnome.org/GNOME/gnumeric/merge_requests/3 . 25 + # Other discussion: 26 + # https://github.com/itstool/itstool/issues/22 27 + # https://github.com/NixOS/nixpkgs/pull/63174 28 + # https://github.com/NixOS/nixpkgs/pull/72342 29 + ./utf8-xmlErrorFuncHandler.patch 30 + ]; 17 31 18 32 outputs = [ "bin" "dev" "out" "man" "doc" ] 19 33 ++ lib.optional pythonSupport "py"
+30
pkgs/development/libraries/libxml2/utf8-xmlErrorFuncHandler.patch
··· 1 + Index: libxml2-2.9.5/python/libxml.c 2 + =================================================================== 3 + --- libxml2-2.9.5.orig/python/libxml.c 4 + +++ libxml2-2.9.5/python/libxml.c 5 + @@ -1620,6 +1620,7 @@ libxml_xmlErrorFuncHandler(ATTRIBUTE_UNU 6 + PyObject *message; 7 + PyObject *result; 8 + char str[1000]; 9 + + unsigned char *ptr = (unsigned char *)str; 10 + 11 + #ifdef DEBUG_ERROR 12 + printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg); 13 + @@ -1636,10 +1637,16 @@ libxml_xmlErrorFuncHandler(ATTRIBUTE_UNU 14 + str[999] = 0; 15 + va_end(ap); 16 + 17 + +#if PY_MAJOR_VERSION >= 3 18 + + /* Ensure the error string doesn't start at UTF8 continuation. */ 19 + + while (*ptr && (*ptr & 0xc0) == 0x80) 20 + + ptr++; 21 + +#endif 22 + + 23 + list = PyTuple_New(2); 24 + PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt); 25 + Py_XINCREF(libxml_xmlPythonErrorFuncCtxt); 26 + - message = libxml_charPtrConstWrap(str); 27 + + message = libxml_charPtrConstWrap(ptr); 28 + PyTuple_SetItem(list, 1, message); 29 + result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list); 30 + Py_XDECREF(list);