don't leak iconv context on OOM
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 10 Jan 2022 14:27:12 +0000 (15:27 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 10 Jan 2022 15:17:13 +0000 (16:17 +0100)
src/stringfunc.cpp

index d611abf..7108bc9 100644 (file)
@@ -795,7 +795,10 @@ std::string iso_to_utf8(const std::string& isostring)
 
    char *buf = (char *)malloc(out_size+1);
    if (buf == NULL)
+   {
+      iconv_close(i2utf8);
       throw runtime_error("out of memory for iconv buffer");
+   }
 
    char *in = (char *)isostring.c_str();
    char *out = buf;
@@ -825,7 +828,10 @@ std::string utf8_to_iso(const std::string& utf8string)
 
    char *buf = (char *)malloc(out_size+1);
    if (buf == NULL)
+   {
+      iconv_close(utf82iso);
       throw runtime_error("out of memory for iconv buffer");
+   }
 
    char *in = (char *)utf8string.c_str();
    char *out = buf;
@@ -853,7 +859,10 @@ wchar_t* utf8_to_wbuf(const std::string& utf8string)
 
    wchar_t *buf = (wchar_t *)malloc(out_size);
    if (buf == NULL)
+   {
+      iconv_close(utf82wstr);
       throw runtime_error("out of memory for iconv buffer");
+   }
 
    char *in = (char *)utf8string.c_str();
    char *out = (char*) buf;
@@ -881,7 +890,10 @@ std::string utf7imap_to_utf8(const std::string& utf7imapstring)
 
    char *buf = (char *)malloc(out_size+1);
    if (buf == NULL)
+   {
+      iconv_close(utf7imap2utf8);
       throw runtime_error("out of memory for iconv buffer");
+   }
 
    char *in = (char *)utf7imapstring.c_str();
    char *out = buf;
@@ -913,7 +925,10 @@ std::string utf8_to_utf7imap(const std::string& utf8string)
 
    char *buf = (char *)malloc(out_size+1);
    if (buf == NULL)
+   {
+      iconv_close(utf82utf7imap);
       throw runtime_error("out of memory for iconv buffer");
+   }
 
    char *in = (char *)utf8string.c_str();
    char *out = buf;