Kelvin Wong avatar Kelvin Wong committed 44986ac Draft

Types to match expected types in PyArg_ParseTupleAndKeywords. Explicit casts in crypto_scrypt call. Using Py_ssize_t per PEP353. Using PY_LONG_LONG macro instead of long long type, see pyport.h. Problem w compiler optimization required setting O-level to zero. On Python Makefile default O3 the hash function segfaults.

Comments (0)

Files changed (3)

 
 includes = []
 library_dirs = []
+CFLAGS = []
 
 if sys.platform.startswith('linux'):
     define_macros = [('HAVE_CLOCK_GETTIME', '1'),
                      ('HAVE_SYS_SYSINFO_H', '1'),
                      ('_FILE_OFFSET_BITS', '64')]
     libraries = ['crypto', 'rt']
+    CFLAGS.append('-O0')
 elif sys.platform.startswith('win32'):
     define_macros = []
     library_dirs = ['c:\OpenSSL-Win32\lib\MinGW']
                                         'scrypt-1.1.6/lib/crypto',
                                         'scrypt-1.1.6/lib/util'] + includes,
                           define_macros=[('HAVE_CONFIG_H', None)] + define_macros,
+                          extra_compile_args=CFLAGS,
                           library_dirs=library_dirs,
                           libraries=libraries)
 
 
 static PyObject *scrypt_hash(PyObject *self, PyObject *args, PyObject* kwargs) {
     PyStringObject *password,   *salt;
-    size_t          passwordlen, saltlen;
+    int passwordlen, saltlen;
     int paramerror, hasherror;
-    uint64_t N = 1 << 14;
-    uint32_t r = 8;
-    uint32_t p = 1;
-    uint8_t *outbuf;
-    size_t   outbuflen;
+    unsigned long long int N = 1 << 14;
+    unsigned long int r = 8;
+    unsigned long int p = 1;
+    unsigned char *outbuf;
+    int outbuflen;
 
     static char *g2_kwlist[] = {"password", "salt", "N", "r", "p", NULL};
 
-    // note, this assumes uint32_t is unsigned long (k)
     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "SS|Kkk", g2_kwlist,
                                                              &password, &salt,
                                                              &N, &r, &p)) {
         hasherror = 0;
     } else {
         paramerror = 0;
-        hasherror = crypto_scrypt((uint8_t *) PyString_AsString((PyObject *) password), passwordlen,
-                                  (uint8_t *) PyString_AsString((PyObject *) salt),     saltlen,
-                                  N, r, p,
-                                  outbuf, outbuflen);
+        hasherror = crypto_scrypt((const uint8_t *) PyString_AsString((PyObject *) password), (size_t) passwordlen,
+                                  (const uint8_t *) PyString_AsString((PyObject *) salt),     (size_t) saltlen,
+                                  (uint64_t) N, (uint32_t) r, (uint32_t) p,
+                                  (uint8_t *) outbuf, (size_t) outbuflen);
     }
 
     Py_END_ALLOW_THREADS;
  * SUCH DAMAGE.
  */
 
+#ifdef PY_SSIZE_T_CLEAN
+#define PY_SSIZE_T_CLEAN
+#endif
+
 #include <Python.h>
 
 #include "scryptenc/scryptenc.h"
     return value;
 }
 static PyObject *scrypt_hash(PyObject *self, PyObject *args, PyObject* kwargs) {
-    const char *password,   *salt;
-    int      passwordlen, saltlen;
+    const char *password, *salt;
+    Py_ssize_t passwordlen, saltlen;
     int paramerror, hasherror;
-    uint64_t N = 1 << 14;
-    uint32_t r = 8;
-    uint32_t p = 1;
-    uint8_t *outbuf;
-    size_t   outbuflen;
+    unsigned PY_LONG_LONG N = 1 << 14;
+    unsigned long r = 8;
+    unsigned long p = 1;
+    unsigned char *outbuf;
+    int outbuflen;
 
     static char *g2_kwlist[] = {"password", "salt", "N", "r", "p", NULL};
 
-    // note, this assumes uint32_t is unsigned long (k)
     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#s#|Kkk", g2_kwlist,
                                      &password, &passwordlen, &salt, &saltlen,
                                      &N, &r, &p)) {
         hasherror = 0;
     } else {
         paramerror = 0;
-        hasherror = crypto_scrypt((const uint8_t *) password, passwordlen,
-                                  (const uint8_t *) salt,     saltlen,
-                                  N, r, p,
-                                  outbuf, outbuflen);
+        hasherror = crypto_scrypt((const uint8_t *) password, (size_t) passwordlen,
+                                  (const uint8_t *) salt,     (size_t) saltlen,
+                                  (uint64_t) N, (uint32_t) r, (uint32_t) p,
+                                  (uint8_t *) outbuf, (size_t) outbuflen);
     }
 
     Py_END_ALLOW_THREADS;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.