From 24a6781687b28e33e05b502c0bab5ed01ea93449 Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Sat, 4 Apr 2020 20:17:14 -0400 Subject: [PATCH] Don't leak internal file on error if mpw_marshal_write file is NULL. --- platform-independent/c/core/src/mpw-marshal.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/platform-independent/c/core/src/mpw-marshal.c b/platform-independent/c/core/src/mpw-marshal.c index 7b93d430..b6ab0730 100644 --- a/platform-independent/c/core/src/mpw-marshal.c +++ b/platform-independent/c/core/src/mpw-marshal.c @@ -695,11 +695,17 @@ const char *mpw_marshal_write( if (!file) return NULL; if (!file->data) { - file->error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate data." }; + if (!file_) + mpw_marshal_file_free( &file ); + else + file->error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate data." }; return NULL; } if (!user->fullName || !strlen( user->fullName )) { - file->error = (MPMarshalError){ MPMarshalErrorMissing, "Missing full name." }; + if (!file_) + mpw_marshal_file_free( &file ); + else + file->error = (MPMarshalError){ MPMarshalErrorMissing, "Missing full name." }; return NULL; } file->error = (MPMarshalError){ MPMarshalSuccess, NULL }; @@ -740,7 +746,10 @@ const char *mpw_marshal_write( // Clear Text mpw_free( &masterKey, MPMasterKeySize ); if (!user->masterKeyProvider || !(masterKey = user->masterKeyProvider( site->algorithm, user->fullName ))) { - file->error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." }; + if (!file_) + mpw_marshal_file_free( &file ); + else + file->error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." }; return NULL; }