8000 gh-111178: fix UBSan failures in `Modules/cjkcodecs/multibytecodec.c` by picnixz · Pull Request #129090 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111178: fix UBSan failures in Modules/cjkcodecs/multibytecodec.c #129090

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 9 commits into from
Jan 25, 2025
Next Next commit
Add casts macros
  • Loading branch information
picnixz committed Jan 20, 2025
commit d5a8c23d84f0fdfd86137c828e037c8254d1781a
16 changes: 16 additions & 0 deletions Modules/cjkcodecs/multibytecodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,27 @@ typedef struct {
PyObject *cjk_module;
} MultibyteCodecObject;

#define _MultibyteCodec_CAST(op) ((MultibyteCodec *)(op))
#define _MultibyteCodecObject_CAST(op) ((MultibyteCodecObject *)(op))
#define MultibyteCodec_Check(state, op) Py_IS_TYPE((op), state->multibytecodec_type)

#define _MultibyteStatefulCodec_HEAD \
PyObject_HEAD \
const MultibyteCodec *codec; \
MultibyteCodec_State state; \
PyObject *errors;

typedef struct {
_MultibyteStatefulCodec_HEAD
} MultibyteStatefulCodecContext;

#define _MultibyteStatefulCodecContext_CAST(op) ((MultibyteStatefulCodecContext *)(op))

#define MAXENCPENDING 2
#define _MultibyteStatefulEncoder_HEAD \
_MultibyteStatefulCodec_HEAD \
PyObject *pending;

typedef struct {
_MultibyteStatefulEncoder_HEAD
} MultibyteStatefulEncoderContext;
Expand All @@ -96,10 +102,14 @@ typedef struct {
_MultibyteStatefulCodec_HEAD \
unsigned char pending[MAXDECPENDING]; \
Py_ssize_t pendingsize;

typedef struct {
_MultibyteStatefulDecoder_HEAD
} MultibyteStatefulDecoderContext;

#define _MultibyteStatefulEncoderContext_CAST(op) ((MultibyteStatefulEncoderContext *)(op))
#define _MultibyteStatefulDecoderContext_CAST(op) ((MultibyteStatefulDecoderContext *)(op))

typedef struct {
_MultibyteStatefulEncoder_HEAD
} MultibyteIncrementalEncoderObject;
Expand All @@ -108,6 +118,9 @@ typedef struct {
_MultibyteStatefulDecoder_HEAD
} MultibyteIncrementalDecoderObject;

#define _MultibyteIncrementalEncoderObject_CAST(op) ((MultibyteIncrementalEncoderObject *)(op))
#define _MultibyteIncrementalDecoderObject_CAST(op) ((MultibyteIncrementalDecoderObject *)(op))

typedef struct {
_MultibyteStatefulDecoder_HEAD
PyObject *stream;
Expand All @@ -118,6 +131,9 @@ typedef struct {
PyObject *stream;
} MultibyteStreamWriterObject;

#define _MultibyteStreamReaderObject_CAST(op) ((MultibyteStreamReaderObject *)(op))
#define _MultibyteStreamWriterObject_CAST(op) ((MultibyteStreamWriterObject *)(op))

/* positive values for illegal sequences */
#define MBERR_TOOSMALL (-1) /* insufficient output buffer space */
#define MBERR_TOOFEW (-2) /* incomplete input buffer */
Expand Down
0