Skip to content

Instantly share code, notes, and snippets.

@shakna-israel
Created June 28, 2020 18:14
Show Gist options
  • Save shakna-israel/dfb34dd7f0e3d8c8b74443b29b20906d to your computer and use it in GitHub Desktop.
Save shakna-israel/dfb34dd7f0e3d8c8b74443b29b20906d to your computer and use it in GitHub Desktop.
Predictable Limits on Python's JSON Parser
#!/bin/sh
# Python will hit it's recursion limit
# If you supply just 4 less than the recursion limit
# I assume this means there's a few objects on the call stack first
# Probably: __main__, print, json.loads, and input.
n="$(python3 -c 'import math; import sys; sys.stdout.write(str(math.floor(sys.getrecursionlimit() - 4)))')"
echo "N: $n"
# Obviously invalid, but unparseable without matching pair
# JSON's grammar is... Not good at being partially parsed.
left="$(yes [ | head -n "$n" | tr -d '\n')"
# Rather than exploding with the expected decodeError
# This will explode with a RecursionError
# Which naturally thrashes the memory cache.
echo "$left" | python3 -c 'import json; print(json.loads(input()))'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment