Skip to content

Fix #181 #183 memory corruption when unpacking unknown enum or repacking enum - #184

Merged
m6w6 merged 1 commit into
msgpack:masterfrom
cracksalad:master
Jun 1, 2026
Merged

m6w6 merged 1 commit into
msgpack:masterfrom
cracksalad:master

Conversation

@cracksalad

@cracksalad cracksalad commented Apr 11, 2026 •

Copy link
Copy Markdown
Contributor

If one unpacks a serialized enum value and...

  • the corresponding enum definition could not be loaded (not required nor autoloadable) or
  • the type name (say test\MyType) is not an enum but a normal class or
  • repacks the same enum (refcount too low)

...there is no warning or error thrown but one would experience memory corruption instead. I am talking about variables having values which are not allowed by their types and something like that - really scary.

Fixes #181 and probably #183 as well.

With regards to the repacking issue, we need to add to the ref count for some reason when unpacking an enum value. That is why I replaced ZVAL_OBJ with ZVAL_OBJ_COPY since the difference is exactly that:

#define ZVAL_OBJ(z, o) do {						\
		zval *__z = (z);						\
		Z_OBJ_P(__z) = (o);						\
		Z_TYPE_INFO_P(__z) = IS_OBJECT_EX;		\
	} while (0)

#define ZVAL_OBJ_COPY(z, o) do {				\
		zval *__z = (z);						\
		zend_object *__o = (o);					\
		GC_ADDREF(__o);							\
		Z_OBJ_P(__z) = __o;						\
		Z_TYPE_INFO_P(__z) = IS_OBJECT_EX;		\
	} while (0)

Excerpt from php/php-src/Zend/zend_types.h#L1138

@codecov-commenter

codecov-commenter commented Apr 11, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.51%. Comparing base (712cc13) to head (35d4381).
⚠️ Report is 86 commits behind head on master.

Files with missing lines Patch % Lines
msgpack_pack.c 0.00% 1 Missing ⚠️
msgpack_unpack.c 88.88% 0 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #184      +/-   ##
==========================================
- Coverage   73.88%   68.51%   -5.37%     
==========================================
  Files           6        7       +1     
  Lines        1191     1350     +159     
  Branches        0      247     +247     
==========================================
+ Hits          880      925      +45     
+ Misses        311      310       -1     
- Partials        0      115     +115     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
@cracksalad cracksalad changed the title Fix memory corruption when unpacking unknown enum Apr 13, 2026
@m6w6
m6w6 merged commit 1055d84 into msgpack:master Jun 1, 2026
11 of 12 checks passed
@cracksalad

Copy link
Copy Markdown
Contributor Author

Thank you very much @m6w6 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants