Skip to content

Commit f428cc6

Browse files
qarkaicaclark
authored andcommitted
Fix memory leak after exif_get_data_as_text() call
Reduce code duplication in related functions. Also fix '_XOPEN_SOURCE redefined' warnings.
1 parent c51ae47 commit f428cc6

File tree

2 files changed

+25
-72
lines changed

2 files changed

+25
-72
lines changed

src/exif-common.cc

+18-53
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
#include <config.h>
2121

2222
#ifdef __linux__
23+
#ifndef _XOPEN_SOURCE
2324
#define _XOPEN_SOURCE
2425
#endif
26+
#endif
2527

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

198-
static gchar *exif_build_formatted_DateTime(ExifData *exif)
200+
static gchar *exif_build_formatted_DateTime(ExifData *exif, const gchar *text_key, const gchar *subsec_key)
199201
{
200202
g_autofree gchar *subsec = nullptr;
201-
gchar buf[128];
202-
gint buflen;
203-
struct tm tm;
204203

205-
gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeOriginal");
204+
gchar *text = exif_get_data_as_text(exif, text_key);
206205
if (text)
207206
{
208-
subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTimeOriginal");
207+
subsec = exif_get_data_as_text(exif, subsec_key);
209208
}
210209
else
211210
{
212211
text = exif_get_data_as_text(exif, "Exif.Image.DateTime");
213-
if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime");
212+
if (!text) return nullptr;
213+
214+
subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime");
214215
}
215216

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

245-
static gchar *exif_build_formatted_DateTimeDigitized(ExifData *exif)
247+
static gchar *exif_build_formatted_DateTime(ExifData *exif)
246248
{
247-
g_autofree gchar *subsec = nullptr;
248-
gchar buf[128];
249-
gint buflen;
250-
struct tm tm;
251-
252-
gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeDigitized");
253-
if (text)
254-
{
255-
subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTimeDigitized");
256-
}
257-
else
258-
{
259-
text = exif_get_data_as_text(exif, "Exif.Image.DateTime");
260-
if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime");
261-
}
262-
263-
/* Convert the stuff into a tm struct */
264-
memset(&tm, 0, sizeof(tm)); /* Uh, strptime could let garbage in tm! */
265-
if (text && strptime(text, "%Y:%m:%d %H:%M:%S", &tm))
266-
{
267-
buflen = strftime(buf, sizeof(buf), "%x %X", &tm);
268-
if (buflen > 0)
269-
{
270-
g_autoptr(GError) error = nullptr;
271-
g_autofree gchar *tmp = g_locale_to_utf8(buf, buflen, nullptr, nullptr, &error);
272-
if (error)
273-
{
274-
log_printf("Error converting locale strftime to UTF-8: %s\n", error->message);
275-
}
276-
else
277-
{
278-
std::swap(text, tmp);
279-
}
280-
}
281-
}
282-
283-
if (subsec)
284-
{
285-
g_autofree gchar *tmp = text;
286-
text = g_strconcat(tmp, ".", subsec, NULL);
287-
}
249+
return exif_build_formatted_DateTime(exif, "Exif.Photo.DateTimeOriginal", "Exif.Photo.SubSecTimeOriginal");
250+
}
288251

289-
return text;
252+
static gchar *exif_build_formatted_DateTimeDigitized(ExifData *exif)
253+
{
254+
return exif_build_formatted_DateTime(exif, "Exif.Photo.DateTimeDigitized", "Exif.Photo.SubSecTimeDigitized");
290255
}
291256

292257
static gchar *exif_build_formatted_ShutterSpeed(ExifData *exif)

src/lua.cc

+7-19
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919
*/
2020

21+
#ifndef _XOPEN_SOURCE
2122
#define _XOPEN_SOURCE
23+
#endif
2224

2325
#include "glua.h"
2426

@@ -232,10 +234,7 @@ static ExifData *lua_check_exif(lua_State *L, int index)
232234
static int lua_exif_get_datum(lua_State *L)
233235
{
234236
const gchar *key;
235-
gchar *value = nullptr;
236237
ExifData *exif;
237-
struct tm tm;
238-
time_t datetime;
239238

240239
exif = lua_check_exif(L, 1);
241240
key = luaL_checkstring(L, 2);
@@ -249,27 +248,16 @@ static int lua_exif_get_datum(lua_State *L)
249248
lua_pushnil(L);
250249
return 1;
251250
}
252-
value = exif_get_data_as_text(exif, key);
253-
if (strcmp(key, "Exif.Photo.DateTimeOriginal") == 0)
254-
{
255-
memset(&tm, 0, sizeof(tm));
256-
if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
257-
{
258-
datetime = mktime(&tm);
259-
lua_pushnumber(L, datetime);
260-
return 1;
261-
}
262251

263-
lua_pushnil(L);
264-
return 1;
265-
}
252+
g_autofree gchar *value = exif_get_data_as_text(exif, key);
266253

267-
if (strcmp(key, "Exif.Photo.DateTimeDigitized") == 0)
254+
if (strcmp(key, "Exif.Photo.DateTimeDigitized") == 0 ||
255+
strcmp(key, "Exif.Photo.DateTimeOriginal") == 0)
268256
{
269-
memset(&tm, 0, sizeof(tm));
257+
std::tm tm{};
270258
if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
271259
{
272-
datetime = mktime(&tm);
260+
const time_t datetime = mktime(&tm);
273261
lua_pushnumber(L, datetime);
274262
return 1;
275263
}

0 commit comments

Comments
 (0)