Skip to content

BUG: Add extra check for failing UTF-8 conversion #32548

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

Conversation

roberthdevries
Copy link
Contributor

@roberthdevries roberthdevries force-pushed the fix-23809-read_excel-crashes-high-surrogate branch from 02cd952 to dc777e5 Compare March 8, 2020 22:10
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Can you also check if there are other issues in the tracker this closes? Shouldn't be specific to Excel given where the fix is made

@@ -34,6 +34,10 @@ int floatify(PyObject *str, double *result, int *maybe_int) {
data = PyBytes_AS_STRING(str);
} else if (PyUnicode_Check(str)) {
tmp = PyUnicode_AsUTF8String(str);
if (tmp == NULL) {
PyErr_SetString(PyExc_TypeError, "UTF8 decode error");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to set the error here? I think the previous call would already set and can just return, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently the 'str' object passed is a Unicode type or a subtype. Ref: https://docs.python.org/3/c-api/unicode.html
And it seems to pass that test fine. However, it fails to convert to UTF8, hence the NULL return value.
BTW, I see know that the message is wrong (decode->encode)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me clarify - so I am asking if you need the PyErr_SetString at all. I think the failing PyUnicode_AsUTF8String call should have already set the global error indicator / message, so I think this is redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed
I tried to figure out if PyUnicode_AsUTF8String sets an exception or not, but I failed to find out.
However a manual test showed an exception so I guess we are covered here.

>>> "\udc88".encode()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'utf-8' codec can't encode character '\udc88' in position 0: surrogates not allowed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that's generally the pattern for the C API functions. Quoting the guide with respect to the global error indicator:

https://docs.python.org/3/c-api/exceptions.html#exception-handling

Most C API functions don’t clear this on success, but will set it to indicate the cause of the error on failure. Most C API functions also return an error indicator, usually NULL if they are supposed to return a pointer, or -1 if they return an integer (exception: the PyArg_*() functions return 1 for success and 0 for failure).

I'm not sure which ones don't fall into the "Most" category, but good to know this one does

@WillAyd WillAyd added the Segfault Non-Recoverable Error label Mar 9, 2020
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation looks good; just need to improve the test a bit

# GH 23809

# should not produce a segmentation violation
pd.read_excel("high_surrogate.xlsx")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create the expected dataframe and use tm.assert_frame_equal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@WillAyd
Copy link
Member

WillAyd commented Mar 10, 2020

Can you check any impact to the CSV reader as well? Based off of where this fix is made I would imagine this impacts that as well as excel, so maybe should add a test / document fix for both

@jreback jreback added the IO Excel read_excel, to_excel label Mar 11, 2020
@jreback
Copy link
Contributor

jreback commented Mar 11, 2020

lgtm. defer to @WillAyd for testing.

@datapythonista datapythonista changed the title Add extra check for failing UTF-8 conversion BUG: Add extra check for failing UTF-8 conversion Mar 11, 2020
Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks

@roberthdevries roberthdevries requested a review from WillAyd March 11, 2020 15:16
@WillAyd WillAyd added this to the 1.1 milestone Mar 12, 2020
@WillAyd WillAyd merged commit 1b76440 into pandas-dev:master Mar 12, 2020
@WillAyd
Copy link
Member

WillAyd commented Mar 12, 2020

Great thanks @roberthdevries

@roberthdevries roberthdevries deleted the fix-23809-read_excel-crashes-high-surrogate branch March 12, 2020 21:36
SeeminSyed pushed a commit to CSCD01-team01/pandas that referenced this pull request Mar 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Excel read_excel, to_excel Segfault Non-Recoverable Error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

read_excel crashes python for certain files
4 participants