Title: fix: avoid memory leak when decoding invalid nested arrays by KowalskiThomas · Pull Request #671 · msgpack/msgpack-python · GitHub
Open Graph Title: fix: avoid memory leak when decoding invalid nested arrays by KowalskiThomas · Pull Request #671 · msgpack/msgpack-python
X Title: fix: avoid memory leak when decoding invalid nested arrays by KowalskiThomas · Pull Request #671 · msgpack/msgpack-python
Description: What is this PR? This PR fixes a memory leak that was detected (accidentally) through fuzzing. The leak happens in some cases when trying to decode invalid data. When decoding an array, the unpacker uses a stack and pushes a new list to that stack for every nested array element. If it eventually reaches a point where the element to decode is problematic, it returns -2 which results in FormatError being raised, but unpack_clear lacks some memory freeing logic (only relevant in the problematic case -- the outermost list is freed but not the other ones) and the objects are lost forever. The leak can directly be observed by running the following reproducer, which pushes several nested lists, and eventually one undefined format byte. It results in returning an error, without freeing the intermediate list objects. The logic is run under tracemalloc, with explicit GC calls to avoid transient still-alive objects that would be freed at some point. The deeper the nesting, the more objects are leaked. import gc import tracemalloc import msgpack._cmsgpack as _c KWARGS = {"raw": False, "strict_map_key": False, "max_array_len": 1 << 20, "max_map_len": 1 << 20} N = 10000 for depth in range(1, 15): data = bytes([0x91] * depth + [0xC1]) gc.collect() tracemalloc.start() s1 = tracemalloc.take_snapshot() for _ in range(N): try: _c.unpackb(data, **KWARGS) except: pass gc.collect() s2 = tracemalloc.take_snapshot() tracemalloc.stop() objs = sum(s.count_diff for s in s2.compare_to(s1, "lineno") if s.count_diff > 0) print(f"depth={depth}: {objs / N:.1f} leaked obj/call")
Open Graph Description: What is this PR? This PR fixes a memory leak that was detected (accidentally) through fuzzing. The leak happens in some cases when trying to decode invalid data. When decoding an array, the unpacke...
X Description: What is this PR? This PR fixes a memory leak that was detected (accidentally) through fuzzing. The leak happens in some cases when trying to decode invalid data. When decoding an array, the unpacke...
Opengraph URL: https://github.com/msgpack/msgpack-python/pull/671
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:90dd5497-e014-4267-4d38-800ca24a4662 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AA86:14453C:B723FB:F72243:6A531BEB |
| html-safe-nonce | 6e2f413df635d9d115a3f1a7c0a195773372ba93230602249839fb3ca4badc31 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTg2OjE0NDUzQzpCNzIzRkI6RjcyMjQzOjZBNTMxQkVCIiwidmlzaXRvcl9pZCI6IjgyMTQyMjg0MjU0NDA5NTk0NjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 637da02b730f7dc6c78f6aaee5f02f5b7fc351ed201118a3a0de35057ee1dc98 |
| hovercard-subject-tag | pull_request:3600972659 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/msgpack/msgpack-python/pull/671/files |
| twitter:image | https://avatars.githubusercontent.com/u/14239160?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/14239160?s=400&v=4 |
| og:image:alt | What is this PR? This PR fixes a memory leak that was detected (accidentally) through fuzzing. The leak happens in some cases when trying to decode invalid data. When decoding an array, the unpacke... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/msgpack/msgpack-python git https://github.com/msgpack/msgpack-python.git |
| octolytics-dimension-user_id | 198264 |
| octolytics-dimension-user_login | msgpack |
| octolytics-dimension-repository_id | 2242705 |
| octolytics-dimension-repository_nwo | msgpack/msgpack-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 2242705 |
| octolytics-dimension-repository_network_root_nwo | msgpack/msgpack-python |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width