Skip to content

bpo-35194: cjkcodec: check the encoded value is not truncated #10432

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

Merged
merged 2 commits into from
Mar 29, 2019

Conversation

izbyshev
Copy link
Contributor

@izbyshev izbyshev commented Nov 9, 2018

@izbyshev izbyshev changed the title bpo-35194: Explicitly check that the encoded byte is in range [0; 255] bpo-35194: Explicitly check that the encoded value is not truncated Nov 9, 2018
do { ((*outbuf)[2]) = (c); } while (0)
#define OUTBYTE4(c) \
do { ((*outbuf)[3]) = (c); } while (0)
#define OUTBYTEI(c, i) \
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 try to convert this macro to a static inline function? I'm trying to slowly convert macros to static inline functions. It avoids uglyness of macros like do { ... } while (0). See https://bugs.python.org/issue35059

For example, a function would require a specific type for c :-) Maybe DBCHAR type?

Copy link
Contributor Author

@izbyshev izbyshev Nov 9, 2018

Choose a reason for hiding this comment

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

The advantage of macros for the purpose of catching conversions changing the mathematical value is that (unsigned char)(c) == (c) checks it for any integer type of c. Note that mathematical values of both operands are never changed in this comparison, at least if we consider C99-conforming compilers and don't take into account platforms where a byte is not 8-bit.

If we used a function instead, c would always be converted to its parameter type. No matter what type we choose, even the widest one, the mathematical value of the argument can be changed (if not because of truncation, than because of conversions between signed and unsigned types). So I don't see how static inline functions can help in this particular issue. Macros may be ugly, but they may also be a powerful tool for specific tasks.

@methane methane changed the title bpo-35194: Explicitly check that the encoded value is not truncated bpo-35194: cjkcodec: check the encoded value is not truncated Mar 29, 2019
@methane methane merged commit 5f45979 into python:master Mar 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants