From 55519670a8711653e7238f0a39eb7e53db9531e4 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 13 Oct 2022 16:25:08 -0500 Subject: [PATCH] Fix parsing long dotenv values Values that exceeded the stack buffer of 64 bytes were incorrectly handled. A new test is added. --- shared-module/dotenv/__init__.c | 16 ++++++++++++---- tests/circuitpython/dotenv_test.env | 12 ++++++++++++ tests/circuitpython/dotenv_test.py | 4 ++++ tests/circuitpython/dotenv_test.py.exp | 12 ++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/shared-module/dotenv/__init__.c b/shared-module/dotenv/__init__.c index be8f2353de..ead7c6561b 100644 --- a/shared-module/dotenv/__init__.c +++ b/shared-module/dotenv/__init__.c @@ -29,6 +29,7 @@ #include "shared-bindings/dotenv/__init__.h" +#include "py/misc.h" #include "py/mpstate.h" #include "py/objstr.h" #include "supervisor/filesystem.h" @@ -272,10 +273,17 @@ mp_obj_t common_hal_dotenv_get_key(const char *path, const char *key) { return mp_const_none; } if ((size_t)actual_len >= sizeof(value)) { - mp_obj_str_t *str = MP_OBJ_TO_PTR(mp_obj_new_str_copy(&mp_type_str, NULL, actual_len + 1)); - dotenv_get_key(path, key, (char *)str->data, actual_len + 1); - str->hash = qstr_compute_hash(str->data, actual_len); - return MP_OBJ_FROM_PTR(str); + byte *buf = m_new(byte, actual_len + 1); + dotenv_get_key(path, key, (char *)buf, actual_len); + buf[actual_len] = 0; + + mp_obj_str_t *o = m_new_obj(mp_obj_str_t); + o->base.type = &mp_type_str; + o->len = actual_len; + o->data = buf; + o->hash = qstr_compute_hash(buf, actual_len); + + return MP_OBJ_FROM_PTR(o); } return mp_obj_new_str(value, actual_len); } diff --git a/tests/circuitpython/dotenv_test.env b/tests/circuitpython/dotenv_test.env index e3bc23d6ce..63753b0ea4 100644 --- a/tests/circuitpython/dotenv_test.env +++ b/tests/circuitpython/dotenv_test.env @@ -23,3 +23,15 @@ quoted_then_comment='value'#comment hash_with_spaces=#value value aa🐍bb=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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx diff --git a/tests/circuitpython/dotenv_test.py b/tests/circuitpython/dotenv_test.py index f217b72dbe..109a3bf221 100644 --- a/tests/circuitpython/dotenv_test.py +++ b/tests/circuitpython/dotenv_test.py @@ -25,3 +25,7 @@ print(f"quoted_then_comment={dotenv.get_key(FILE, 'quoted_then_comment')}") print(f"hash_with_spaces={dotenv.get_key(FILE, 'hash_with_spaces')}") print(f"aa🐍bb={dotenv.get_key(FILE, 'aa🐍bb')}") print(f"value_with_emoji={dotenv.get_key(FILE, 'value_with_emoji')}") + +for i in range(12): + key = f"sz{i}" + print(f"len({key})={len(dotenv.get_key(FILE, key))}") diff --git a/tests/circuitpython/dotenv_test.py.exp b/tests/circuitpython/dotenv_test.py.exp index d67d570999..e0ed5f1505 100644 --- a/tests/circuitpython/dotenv_test.py.exp +++ b/tests/circuitpython/dotenv_test.py.exp @@ -21,3 +21,15 @@ quoted_then_comment=value hash_with_spaces=#value value aa🐍bb=key with emoji value_with_emoji=aa🐍bb +len(sz0)=1 +len(sz1)=2 +len(sz2)=4 +len(sz3)=8 +len(sz4)=16 +len(sz5)=32 +len(sz6)=64 +len(sz7)=128 +len(sz8)=256 +len(sz9)=512 +len(sz10)=1024 +len(sz11)=2048