-
Notifications
You must be signed in to change notification settings - Fork 108
libxml2 version check #299
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
#include "common.h" | ||
#include "lxml.h" | ||
#include "exception.h" | ||
|
||
#include <etree_defs.h> | ||
#include <etree_api.h> | ||
|
@@ -17,8 +18,85 @@ | |
#include <libxml/parser.h> | ||
#include <libxml/dict.h> | ||
|
||
#define XMLSEC_EXTRACT_VERSION(x, y) ((x / (y)) % 100) | ||
|
||
#define XMLSEC_EXTRACT_MAJOR(x) XMLSEC_EXTRACT_VERSION(x, 100 * 100) | ||
#define XMLSEC_EXTRACT_MINOR(x) XMLSEC_EXTRACT_VERSION(x, 100) | ||
#define XMLSEC_EXTRACT_PATCH(x) XMLSEC_EXTRACT_VERSION(x, 1) | ||
|
||
static long PyXmlSec_GetLibXmlVersionLong() { | ||
return PyOS_strtol(xmlParserVersion, NULL, 10); | ||
} | ||
long PyXmlSec_GetLibXmlVersionMajor() { | ||
return XMLSEC_EXTRACT_MAJOR(PyXmlSec_GetLibXmlVersionLong()); | ||
} | ||
long PyXmlSec_GetLibXmlVersionMinor() { | ||
return XMLSEC_EXTRACT_MINOR(PyXmlSec_GetLibXmlVersionLong()); | ||
} | ||
long PyXmlSec_GetLibXmlVersionPatch() { | ||
return XMLSEC_EXTRACT_PATCH(PyXmlSec_GetLibXmlVersionLong()); | ||
} | ||
|
||
long PyXmlSec_GetLibXmlCompiledVersionMajor() { | ||
return XMLSEC_EXTRACT_MAJOR(LIBXML_VERSION); | ||
} | ||
long PyXmlSec_GetLibXmlCompiledVersionMinor() { | ||
return XMLSEC_EXTRACT_MINOR(LIBXML_VERSION); | ||
} | ||
long PyXmlSec_GetLibXmlCompiledVersionPatch() { | ||
return XMLSEC_EXTRACT_PATCH(LIBXML_VERSION); | ||
} | ||
|
||
static int PyXmlSec_CheckLxmlLibraryVersion(void) { | ||
// Make sure that the version of libxml2 lxml is using is the same as the one we are using. Because | ||
// we pass trees between the two libraries, we need to make sure that they are using the same version | ||
// of libxml2, or we could run into difficult to debug segfaults. | ||
// See: https://github.com/xmlsec/python-xmlsec/issues/283 | ||
|
||
PyObject* lxml = NULL; | ||
PyObject* version = NULL; | ||
|
||
// Default to failure | ||
int result = -1; | ||
|
||
lxml = PyImport_ImportModule("lxml.etree"); | ||
if (lxml == NULL) { | ||
goto FINALIZE; | ||
} | ||
version = PyObject_GetAttrString(lxml, "LIBXML_VERSION"); | ||
if (version == NULL) { | ||
goto FINALIZE; | ||
} | ||
if (!PyTuple_Check(version) || PyTuple_Size(version) != 3) { | ||
goto FINALIZE; | ||
} | ||
|
||
PyObject* major = PyTuple_GetItem(version, 0); | ||
PyObject* minor = PyTuple_GetItem(version, 1); | ||
|
||
if (!PyLong_Check(major) || !PyLong_Check(minor)) { | ||
goto FINALIZE; | ||
} | ||
|
||
if (PyLong_AsLong(major) != PyXmlSec_GetLibXmlVersionMajor() || PyLong_AsLong(minor) != PyXmlSec_GetLibXmlVersionMinor()) { | ||
goto FINALIZE; | ||
} | ||
Comment on lines
+74
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this should usually work, users can easily assign tuples with large integers to I suggest adding an exception check also to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought that |
||
|
||
result = 0; | ||
|
||
FINALIZE: | ||
// Cleanup our references, and return the result | ||
Py_XDECREF(lxml); | ||
Py_XDECREF(version); | ||
return result; | ||
} | ||
Comment on lines
+87
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should call |
||
|
||
int PyXmlSec_InitLxmlModule(void) { | ||
if (PyXmlSec_CheckLxmlLibraryVersion() < 0) { | ||
PyXmlSec_SetLastError("lxml & xmlsec libxml2 library version mismatch"); | ||
return -1; | ||
} | ||
|
||
return import_lxml__etree(); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably enough to check "size < 2" here, if all you look at is the first two entries.