Skip to content

Commit

Permalink
Fix memory leak after exif_get_data_as_text() call
Browse files Browse the repository at this point in the history
Reduce code duplication in related functions.
Also fix '_XOPEN_SOURCE redefined' warnings.
  • Loading branch information
qarkai committed Jan 13, 2025
1 parent c51ae47 commit f1eb574
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 72 deletions.
71 changes: 18 additions & 53 deletions src/exif-common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
#include <config.h>

#ifdef __linux__
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif
#endif

#include <sys/stat.h>
#include <sys/types.h>
Expand Down Expand Up @@ -195,29 +197,29 @@ static gchar *exif_build_formatted_Camera(ExifData *exif)
(software2 && (make || model2)) ? ")" : "");
}

static gchar *exif_build_formatted_DateTime(ExifData *exif)
static gchar *exif_build_formatted_DateTime(ExifData *exif, const gchar *text_key, const gchar *subsec_key)
{
g_autofree gchar *subsec = nullptr;
gchar buf[128];
gint buflen;
struct tm tm;

gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeOriginal");
gchar *text = exif_get_data_as_text(exif, text_key);
if (text)
{
subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTimeOriginal");
subsec = exif_get_data_as_text(exif, subsec_key);
}
else
{
text = exif_get_data_as_text(exif, "Exif.Image.DateTime");
if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime");
if (!text) return nullptr;

subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime");
}

/* Convert the stuff into a tm struct */
memset(&tm, 0, sizeof(tm)); /* Uh, strptime could let garbage in tm! */
if (text && strptime(text, "%Y:%m:%d %H:%M:%S", &tm))
std::tm tm{}; /* Uh, strptime could let garbage in tm! */
if (strptime(text, "%Y:%m:%d %H:%M:%S", &tm))
{
buflen = strftime(buf, sizeof(buf), "%x %X", &tm);
gchar buf[128];
const gint buflen = strftime(buf, sizeof(buf), "%x %X", &tm);
if (buflen > 0)
{
g_autoptr(GError) error = nullptr;
Expand All @@ -242,51 +244,14 @@ static gchar *exif_build_formatted_DateTime(ExifData *exif)
return text;
}

static gchar *exif_build_formatted_DateTimeDigitized(ExifData *exif)
static gchar *exif_build_formatted_DateTime(ExifData *exif)
{
g_autofree gchar *subsec = nullptr;
gchar buf[128];
gint buflen;
struct tm tm;

gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeDigitized");
if (text)
{
subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTimeDigitized");
}
else
{
text = exif_get_data_as_text(exif, "Exif.Image.DateTime");
if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime");
}

/* Convert the stuff into a tm struct */
memset(&tm, 0, sizeof(tm)); /* Uh, strptime could let garbage in tm! */
if (text && strptime(text, "%Y:%m:%d %H:%M:%S", &tm))
{
buflen = strftime(buf, sizeof(buf), "%x %X", &tm);
if (buflen > 0)
{
g_autoptr(GError) error = nullptr;
g_autofree gchar *tmp = g_locale_to_utf8(buf, buflen, nullptr, nullptr, &error);
if (error)
{
log_printf("Error converting locale strftime to UTF-8: %s\n", error->message);
}
else
{
std::swap(text, tmp);
}
}
}

if (subsec)
{
g_autofree gchar *tmp = text;
text = g_strconcat(tmp, ".", subsec, NULL);
}
return exif_build_formatted_DateTime(exif, "Exif.Photo.DateTimeOriginal", "Exif.Photo.SubSecTimeOriginal");
}

return text;
static gchar *exif_build_formatted_DateTimeDigitized(ExifData *exif)
{
return exif_build_formatted_DateTime(exif, "Exif.Photo.DateTimeDigitized", "Exif.Photo.SubSecTimeDigitized");
}

static gchar *exif_build_formatted_ShutterSpeed(ExifData *exif)
Expand Down
26 changes: 7 additions & 19 deletions src/lua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif

#include "glua.h"

Expand Down Expand Up @@ -232,10 +234,7 @@ static ExifData *lua_check_exif(lua_State *L, int index)
static int lua_exif_get_datum(lua_State *L)
{
const gchar *key;
gchar *value = nullptr;
ExifData *exif;
struct tm tm;
time_t datetime;

exif = lua_check_exif(L, 1);
key = luaL_checkstring(L, 2);
Expand All @@ -249,27 +248,16 @@ static int lua_exif_get_datum(lua_State *L)
lua_pushnil(L);
return 1;
}
value = exif_get_data_as_text(exif, key);
if (strcmp(key, "Exif.Photo.DateTimeOriginal") == 0)
{
memset(&tm, 0, sizeof(tm));
if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
{
datetime = mktime(&tm);
lua_pushnumber(L, datetime);
return 1;
}

lua_pushnil(L);
return 1;
}
g_autofree gchar *value = exif_get_data_as_text(exif, key);

if (strcmp(key, "Exif.Photo.DateTimeDigitized") == 0)
if (strcmp(key, "Exif.Photo.DateTimeDigitized") == 0 ||
strcmp(key, "Exif.Photo.DateTimeOriginal") == 0)
{
memset(&tm, 0, sizeof(tm));
std::tm tm{};
if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
{
datetime = mktime(&tm);
const time_t datetime = mktime(&tm);
lua_pushnumber(L, datetime);
return 1;
}
Expand Down

0 comments on commit f1eb574

Please sign in to comment.