From 1ef1ebbf8d182953c08b00d2193738e4220eb495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Berder?= Date: Sat, 19 Jan 2019 08:28:40 +0000 Subject: [PATCH] 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 * Core: Fix memory leak if gettimeofday or timespec_get failed Signed-off-by: Francois Berder * Core: Protect against malloc failure in mCoreSaveStateNamed Signed-off-by: Francois Berder --- src/core/serialize.c | 45 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/core/serialize.c b/src/core/serialize.c index c6a29b242..5113980ec 100644 --- a/src/core/serialize.c +++ b/src/core/serialize.c @@ -306,31 +306,36 @@ bool mCoreSaveStateNamed(struct mCore* core, struct VFile* vf, int flags) { if (flags & SAVESTATE_METADATA) { uint64_t* creationUsec = malloc(sizeof(*creationUsec)); + if (creationUsec) { #ifndef _MSC_VER - struct timeval tv; - if (!gettimeofday(&tv, 0)) { - uint64_t usec = tv.tv_usec; - usec += tv.tv_sec * 1000000LL; - STORE_64LE(usec, 0, creationUsec); - } + struct timeval tv; + if (!gettimeofday(&tv, 0)) { + uint64_t usec = tv.tv_usec; + usec += tv.tv_sec * 1000000LL; + STORE_64LE(usec, 0, creationUsec); + } #else - struct timespec ts; - if (timespec_get(&ts, TIME_UTC)) { - uint64_t usec = ts.tv_nsec / 1000; - usec += ts.tv_sec * 1000000LL; - STORE_64LE(usec, 0, creationUsec); - } + struct timespec ts; + if (timespec_get(&ts, TIME_UTC)) { + uint64_t usec = ts.tv_nsec / 1000; + usec += ts.tv_sec * 1000000LL; + STORE_64LE(usec, 0, creationUsec); + } #endif - else { - creationUsec = 0; + else { + free(creationUsec); + creationUsec = 0; + } } - struct mStateExtdataItem item = { - .size = sizeof(*creationUsec), - .data = creationUsec, - .clean = free - }; - mStateExtdataPut(&extdata, EXTDATA_META_TIME, &item); + if (creationUsec) { + struct mStateExtdataItem item = { + .size = sizeof(*creationUsec), + .data = creationUsec, + .clean = free + }; + mStateExtdataPut(&extdata, EXTDATA_META_TIME, &item); + } } if (flags & SAVESTATE_SAVEDATA) {