Skip to content

avoid os.stat() int overflow on smallint-only builds #2626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions extmod/vfs_fat.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,24 +345,31 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
} else {
mode |= MP_S_IFREG;
}
mp_uint_t seconds = timeutils_seconds_since_epoch(
1980 + ((fno.fdate >> 9) & 0x7f),
(fno.fdate >> 5) & 0x0f,
fno.fdate & 0x1f,
(fno.ftime >> 11) & 0x1f,
(fno.ftime >> 5) & 0x3f,
2 * (fno.ftime & 0x1f)
);
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
// On non-longint builds, the number of seconds since 1970 (epoch) is too
// large to fit in a smallint, so just return 31-DEC-1999 (0).
mp_obj_t seconds = MP_OBJ_NEW_SMALL_INT(946684800);
#else
mp_obj_t seconds = mp_obj_new_int_from_uint(
timeutils_seconds_since_epoch(
1980 + ((fno.fdate >> 9) & 0x7f),
(fno.fdate >> 5) & 0x0f,
fno.fdate & 0x1f,
(fno.ftime >> 11) & 0x1f,
(fno.ftime >> 5) & 0x3f,
2 * (fno.ftime & 0x1f)
));
#endif
t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode
t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
t->items[6] = mp_obj_new_int_from_uint(fno.fsize); // st_size
t->items[7] = mp_obj_new_int_from_uint(seconds); // st_atime
t->items[8] = mp_obj_new_int_from_uint(seconds); // st_mtime
t->items[9] = mp_obj_new_int_from_uint(seconds); // st_ctime
t->items[7] = seconds; // st_atime
t->items[8] = seconds; // st_mtime
t->items[9] = seconds; // st_ctime

return MP_OBJ_FROM_PTR(t);
}
Expand Down
5 changes: 5 additions & 0 deletions shared-bindings/os/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir);
//|
//| Get the status of a file or directory.
//|
//| .. note:: On builds without long integers, the number of seconds
//| for contemporary dates will not fit in a small integer.
//| So the time fields return 946684800,
//| which is the number of seconds corresponding to 1999-12-31.
//|
mp_obj_t os_stat(mp_obj_t path_in) {
const char *path = mp_obj_str_get_str(path_in);
return common_hal_os_stat(path);
Expand Down