Discussion:
[PATCH] Use a separate Py_ssize_t var instead of casting an int pointer as a Py_ssize_t pointer.
Jordan Rupprecht
2018-09-20 18:05:56 UTC
Permalink
Casting is fine if Py_ssize_t == int, but not when Py_ssize_t == long.

Signed-off-by: Jordan Rupprecht <***@google.com>
---
python/ftdi1.i | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/python/ftdi1.i b/python/ftdi1.i
index 93793f8..d53ebb0 100644
--- a/python/ftdi1.i
+++ b/python/ftdi1.i
@@ -22,11 +22,13 @@ inline PyObject* charp2str(const char *v_, long len)
inline char * str2charp_size(PyObject* pyObj, int * size)
{
char * v_ = 0;
+ Py_ssize_t len = 0;
#if PY_MAJOR_VERSION >= 3
- PyBytes_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size);
+ PyBytes_AsStringAndSize(pyObj, &v_, &len);
#else
- PyString_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size);
+ PyString_AsStringAndSize(pyObj, &v_, &len);
#endif
+ *size = len;
return v_;
}
%}
--
2.19.0.444.g18242da7ef-goog


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+***@developer.intra2net.com
Jordan Rupprecht
2018-10-02 21:04:06 UTC
Permalink
Ping -- does anyone have time to take a look at this patch/merge it in
to master if it looks good?
Post by Jordan Rupprecht
Casting is fine if Py_ssize_t == int, but not when Py_ssize_t == long.
---
python/ftdi1.i | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/python/ftdi1.i b/python/ftdi1.i
index 93793f8..d53ebb0 100644
--- a/python/ftdi1.i
+++ b/python/ftdi1.i
@@ -22,11 +22,13 @@ inline PyObject* charp2str(const char *v_, long len)
inline char * str2charp_size(PyObject* pyObj, int * size)
{
char * v_ = 0;
+ Py_ssize_t len = 0;
#if PY_MAJOR_VERSION >= 3
- PyBytes_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size);
+ PyBytes_AsStringAndSize(pyObj, &v_, &len);
#else
- PyString_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size);
+ PyString_AsStringAndSize(pyObj, &v_, &len);
#endif
+ *size = len;
return v_;
}
%}
--
2.19.0.444.g18242da7ef-goog
Thomas Jarosch
2018-10-04 08:49:13 UTC
Permalink
Hello Jordan,
Post by Jordan Rupprecht
Ping -- does anyone have time to take a look at this patch/merge it in
to master if it looks good?
I hope to look this and the other outstanding patches pretty soon.
Sorry for the delay, I was on vacation when the patch was submitted.

Cheers,
Thomas




--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+***@developer.intra2net.com
Thomas Jarosch
2018-11-05 21:28:12 UTC
Permalink
Hi Jordan,
Post by Jordan Rupprecht
Casting is fine if Py_ssize_t == int, but not when Py_ssize_t == long.
---
python/ftdi1.i | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/python/ftdi1.i b/python/ftdi1.i
index 93793f8..d53ebb0 100644
--- a/python/ftdi1.i
+++ b/python/ftdi1.i
@@ -22,11 +22,13 @@ inline PyObject* charp2str(const char *v_, long len)
inline char * str2charp_size(PyObject* pyObj, int * size)
{
char * v_ = 0;
+ Py_ssize_t len = 0;
#if PY_MAJOR_VERSION >= 3
- PyBytes_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size);
+ PyBytes_AsStringAndSize(pyObj, &v_, &len);
#else
- PyString_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size);
+ PyString_AsStringAndSize(pyObj, &v_, &len);
#endif
+ *size = len;
return v_;
}
%}
patch applied, thanks Jordan!

Were you hit by this during runtime?


@all: One patch from Uwe Bonnes and one from Eric L. Schott are still
outstanding. I have to take care of them on another day,
but they won't be forgotten.

Cheers,
Thomas

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+***@developer.intra2net.com
Jordan Rupprecht
2018-11-05 23:01:51 UTC
Permalink
On Mon, Nov 5, 2018 at 1:28 PM Thomas Jarosch
Post by Thomas Jarosch
Hi Jordan,
Post by Jordan Rupprecht
Casting is fine if Py_ssize_t == int, but not when Py_ssize_t == long.
---
python/ftdi1.i | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/python/ftdi1.i b/python/ftdi1.i
index 93793f8..d53ebb0 100644
--- a/python/ftdi1.i
+++ b/python/ftdi1.i
@@ -22,11 +22,13 @@ inline PyObject* charp2str(const char *v_, long len)
inline char * str2charp_size(PyObject* pyObj, int * size)
{
char * v_ = 0;
+ Py_ssize_t len = 0;
#if PY_MAJOR_VERSION >= 3
- PyBytes_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size);
+ PyBytes_AsStringAndSize(pyObj, &v_, &len);
#else
- PyString_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size);
+ PyString_AsStringAndSize(pyObj, &v_, &len);
#endif
+ *size = len;
return v_;
}
%}
patch applied, thanks Jordan!
Thanks!
Post by Thomas Jarosch
Were you hit by this during runtime?
Yep. It was caught in a unit test, but only with some very specific
flags (I think it was a combination of -O3 and -fPIE, among other
things).

I'm not actually familiar with the details of the test itself, but we
observed a strange segfault when bumping up the version of clang that
we use. We found the root cause for a few other failures we had (and
I'm not sure that we confirmed the libftdi error had the same root
cause, but it's likely) to a change that reduced unnecessary padding
around certain pieces of data. Usually the problems with the code were
a little more obvious, like:
const char foo[] = {'f', 'o', 'o'};
int bar() { return strlen(foo); }
Which previously returned 3 because there was "accidental" padding,
but now returns a much larger number depending on how long it takes to
get to null bytes in memory...
Anyway, I imagine something similar could be the explanation of doing
a C-style cast on different types here. But to be honest, once the
test started working again, I didn't bother digging too much :)

The llvm revision that gave us other failures, and probably this one,
is https://reviews.llvm.org/rL342053.
Post by Thomas Jarosch
@all: One patch from Uwe Bonnes and one from Eric L. Schott are still
outstanding. I have to take care of them on another day,
but they won't be forgotten.
Cheers,
Thomas
Loading...