mirror of https://github.com/mgba-emu/mgba.git
Core: Improve handling failures in mCoreSaveStateNamed function (#1272)
* Core: Do not set time in save state if we could not get the time Signed-off-by: Francois Berder <fberder@outlook.fr> * Core: Fix memory leak if gettimeofday or timespec_get failed Signed-off-by: Francois Berder <fberder@outlook.fr> * Core: Protect against malloc failure in mCoreSaveStateNamed Signed-off-by: Francois Berder <fberder@outlook.fr>
This commit is contained in:
parent
6f336ce0ec
commit
6fecc6d379
|
@ -306,31 +306,36 @@ bool mCoreSaveStateNamed(struct mCore* core, struct VFile* vf, int flags) {
|
||||||
|
|
||||||
if (flags & SAVESTATE_METADATA) {
|
if (flags & SAVESTATE_METADATA) {
|
||||||
uint64_t* creationUsec = malloc(sizeof(*creationUsec));
|
uint64_t* creationUsec = malloc(sizeof(*creationUsec));
|
||||||
|
if (creationUsec) {
|
||||||
#ifndef _MSC_VER
|
#ifndef _MSC_VER
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
if (!gettimeofday(&tv, 0)) {
|
if (!gettimeofday(&tv, 0)) {
|
||||||
uint64_t usec = tv.tv_usec;
|
uint64_t usec = tv.tv_usec;
|
||||||
usec += tv.tv_sec * 1000000LL;
|
usec += tv.tv_sec * 1000000LL;
|
||||||
STORE_64LE(usec, 0, creationUsec);
|
STORE_64LE(usec, 0, creationUsec);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
if (timespec_get(&ts, TIME_UTC)) {
|
if (timespec_get(&ts, TIME_UTC)) {
|
||||||
uint64_t usec = ts.tv_nsec / 1000;
|
uint64_t usec = ts.tv_nsec / 1000;
|
||||||
usec += ts.tv_sec * 1000000LL;
|
usec += ts.tv_sec * 1000000LL;
|
||||||
STORE_64LE(usec, 0, creationUsec);
|
STORE_64LE(usec, 0, creationUsec);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
else {
|
else {
|
||||||
creationUsec = 0;
|
free(creationUsec);
|
||||||
|
creationUsec = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct mStateExtdataItem item = {
|
if (creationUsec) {
|
||||||
.size = sizeof(*creationUsec),
|
struct mStateExtdataItem item = {
|
||||||
.data = creationUsec,
|
.size = sizeof(*creationUsec),
|
||||||
.clean = free
|
.data = creationUsec,
|
||||||
};
|
.clean = free
|
||||||
mStateExtdataPut(&extdata, EXTDATA_META_TIME, &item);
|
};
|
||||||
|
mStateExtdataPut(&extdata, EXTDATA_META_TIME, &item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & SAVESTATE_SAVEDATA) {
|
if (flags & SAVESTATE_SAVEDATA) {
|
||||||
|
|
Loading…
Reference in New Issue