Merge pull request #7395 from jepler/fix-getenv_int-dos-newlines

Fix getenv_int handling of "dos-style" newlines
This commit is contained in:
Dan Halbert 2022-12-28 18:32:38 -05:00 committed by GitHub
commit 70b838a42d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 9 deletions

View File

@ -57,7 +57,11 @@
#endif
#if defined(MICROPY_UNIX_COVERAGE)
#include "py/objstr.h"
typedef int os_getenv_err_t;
mp_obj_t common_hal_os_getenv(const char *key, mp_obj_t default_);
os_getenv_err_t common_hal_os_getenv_str(const char *key, char *value, size_t value_len);
os_getenv_err_t common_hal_os_getenv_int(const char *key, mp_int_t *value);
#endif
STATIC mp_obj_t mod_os_urandom(mp_obj_t num) {
@ -212,6 +216,28 @@ STATIC mp_obj_t mod_os_getenv(mp_obj_t var_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(mod_os_getenv_obj, mod_os_getenv);
#if defined(MICROPY_UNIX_COVERAGE)
STATIC mp_obj_t mod_os_getenv_int(mp_obj_t var_in) {
mp_int_t value;
os_getenv_err_t result = common_hal_os_getenv_int(mp_obj_str_get_str(var_in), &value);
if (result == 0) {
return mp_obj_new_int(value);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(mod_os_getenv_int_obj, mod_os_getenv_int);
STATIC mp_obj_t mod_os_getenv_str(mp_obj_t var_in) {
char buf[4096];
os_getenv_err_t result = common_hal_os_getenv_str(mp_obj_str_get_str(var_in), buf, sizeof(buf));
if (result == 0) {
return mp_obj_new_str_copy(&mp_type_str, (byte *)buf, strlen(buf));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(mod_os_getenv_str_obj, mod_os_getenv_str);
#endif
STATIC mp_obj_t mod_os_putenv(mp_obj_t key_in, mp_obj_t value_in) {
const char *key = mp_obj_str_get_str(key_in);
const char *value = mp_obj_str_get_str(value_in);
@ -351,6 +377,10 @@ STATIC const mp_rom_map_elem_t mp_module_os_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&mod_os_rename_obj) },
{ MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&mod_os_rmdir_obj) },
{ MP_ROM_QSTR(MP_QSTR_getenv), MP_ROM_PTR(&mod_os_getenv_obj) },
#if defined(MICROPY_UNIX_COVERAGE)
{ MP_ROM_QSTR(MP_QSTR_getenv_int), MP_ROM_PTR(&mod_os_getenv_int_obj) },
{ MP_ROM_QSTR(MP_QSTR_getenv_str), MP_ROM_PTR(&mod_os_getenv_str_obj) },
#endif
{ MP_ROM_QSTR(MP_QSTR_putenv), MP_ROM_PTR(&mod_os_putenv_obj) },
{ MP_ROM_QSTR(MP_QSTR_unsetenv), MP_ROM_PTR(&mod_os_unsetenv_obj) },
{ MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mod_os_mkdir_obj) },

View File

@ -37,6 +37,10 @@
// These are defined in modos.c
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mod_os_errno_obj);
MP_DECLARE_CONST_FUN_OBJ_1(mod_os_getenv_obj);
#if defined(MICROPY_UNIX_COVERAGE)
MP_DECLARE_CONST_FUN_OBJ_1(mod_os_getenv_int_obj);
MP_DECLARE_CONST_FUN_OBJ_1(mod_os_getenv_str_obj);
#endif
MP_DECLARE_CONST_FUN_OBJ_1(mod_os_putenv_obj);
MP_DECLARE_CONST_FUN_OBJ_1(mod_os_unsetenv_obj);
MP_DECLARE_CONST_FUN_OBJ_1(mod_os_system_obj);
@ -47,6 +51,10 @@ STATIC const mp_rom_map_elem_t uos_vfs_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_errno), MP_ROM_PTR(&mod_os_errno_obj) },
{ MP_ROM_QSTR(MP_QSTR_getenv), MP_ROM_PTR(&mod_os_getenv_obj) },
#if defined(MICROPY_UNIX_COVERAGE)
{ MP_ROM_QSTR(MP_QSTR_getenv_int), MP_ROM_PTR(&mod_os_getenv_int_obj) },
{ MP_ROM_QSTR(MP_QSTR_getenv_str), MP_ROM_PTR(&mod_os_getenv_str_obj) },
#endif
{ MP_ROM_QSTR(MP_QSTR_putenv), MP_ROM_PTR(&mod_os_putenv_obj) },
{ MP_ROM_QSTR(MP_QSTR_unsetenv), MP_ROM_PTR(&mod_os_unsetenv_obj) },
{ MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&mod_os_system_obj) },

View File

@ -292,6 +292,7 @@ STATIC os_getenv_err_t os_getenv_vstr(const char *path, const char *key, vstr_t
while (!is_eof(&active_file)) {
if (key_matches(&active_file, key)) {
result = read_value(&active_file, buf, quoted);
break;
}
}
close_file(&active_file);
@ -413,6 +414,9 @@ STATIC os_getenv_err_t common_hal_os_getenv_int_inner(const char *key, mp_int_t
}
char *end;
long num = strtol(buf, &end, 0);
while (unichar_isspace(*end)) {
end++;
}
if (end == buf || *end) { // If the whole buffer was not consumed it's an error
return GETENV_ERR_UNEXPECTED | *end;
}

View File

@ -39,11 +39,11 @@ content_good = b"""
# comment
key0 = "hello world"
key1 = 7
key2= "\n"
key3 ="\u00c1x"
key4 = "\U000000c1x"
key5 = "\f\"\\"
key6 = "\t\r\b"
key2= "\\n"
key3 ="\\u00c1x"
key4 = "\\U000000c1x"
key5 = "\\f\\"\\\\"
key6 = "\\t\\r\\b"
key7 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
key8 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
key9 = "hello comment" # comment
@ -79,6 +79,9 @@ content_good = content_good.replace(b"\n", b"\r\n")
for i in range(13):
run_test(f"key{i}", content_good)
run_test(f"K", b"K = 7\r\n")
print(uos.getenv_int("K"))
# Test value without trailing newline
run_test(f"noeol", b"noeol=3")

View File

@ -1,9 +1,9 @@
key0 'hello world'
key1 7
key2 Invalid byte '\n'
key2 '\n'
key3 'Áx'
key4 'Áx'
key5 Invalid byte '\\'
key5 '\x0c"\\'
key6 '\t\r\x08'
key7 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
key8 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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