On Monday 20 March 2017, Andi Vajda wrote:
> On Mon, 20 Mar 2017, Ruediger Meier wrote:

> >> Someone with access to Windows, please help test/fix/finish
> >> support for Python 3 on Windows, both with the MSVC and Mingw
> >> compilers. I have no access to Windows anymore.
> >
> > I know already about one MSVC issue:
> > https://github.com/rudimeier/jcc/issues/1
> >
> > probably fixed by
> > https://github.com/rudimeier/jcc/commit/764ed0dc1f77c68e4a6998688d2
> >e8340704fd237 (But this fix is also not tested yet.)
>
> I changed strhash to use Py_hash_t.

This is now wrong and I could reproduce a segfault on OSX 10.11, xcode 
8. The buffersize "hexdig + 1" has to match the type we are printing. 
We can't calculate the size from Py_hash_t but print ulong.

Most safely and without precision loss we could do it like the patch 
below.

Notes:
1. "static const" was required to actually fix MSVC's VLA issue.
2. The macro PRIxMAX is the same as "%jx". I've choosed the macro 
because it should be compatible to Visual Studio >=2013 while "%jx" 
would need Visual Studio >=2015. Moreover when using incompatible 
compilers the macro would give an error at compile time rather 
than "%jx" would just crash at runtime.


--------
diff --git a/jcc3/sources/jcc.cpp b/jcc3/sources/jcc.cpp
index 8c12f00..90baa8b 100644
--- a/jcc3/sources/jcc.cpp
+++ b/jcc3/sources/jcc.cpp
@@ -15,6 +15,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <inttypes.h>
 #include <jni.h>

 #ifdef linux
@@ -194,11 +195,11 @@ static PyObject *t_jccenv_isShared(PyObject *self)

 static PyObject *t_jccenv_strhash(PyObject *self, PyObject *arg)
 {
-    Py_hash_t hash = PyObject_Hash(arg);
-    size_t hexdig = sizeof(Py_hash_t) * 2;
+    uintmax_t hash = (uintmax_t) PyObject_Hash(arg);
+    static const size_t hexdig = sizeof(hash) * 2;
     char buffer[hexdig + 1];

-    sprintf(buffer, "%0*lx", (int) hexdig, (unsigned long) hash);
+    sprintf(buffer, "%0*"PRIxMAX, (int) hexdig, hash);
     return PyUnicode_FromStringAndSize(buffer, hexdig);
 }
--------------

cu,
Rudi

Reply via email to