diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2013-03-17 12:38:44 +0100 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2013-03-17 12:38:44 +0100 |
| commit | db74836c3a803004b5e1e66a57790b3d127eeb77 (patch) | |
| tree | c02ca69c10c7af4186b6c5048a76d4f470c6c24e /src | |
| parent | 41e84de578256482c7705e531c37572982a90195 (diff) | |
| download | cataract-db74836c3a803004b5e1e66a57790b3d127eeb77.tar.xz | |
Use reentrant versions of datetime functions for thread safety
Diffstat (limited to 'src')
| -rw-r--r-- | src/jpeg-utils.cpp | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/src/jpeg-utils.cpp b/src/jpeg-utils.cpp index c9c080d..6af7f1f 100644 --- a/src/jpeg-utils.cpp +++ b/src/jpeg-utils.cpp @@ -39,7 +39,6 @@ static void shift_time (struct tm *tm, int offset_min) { time_t t; - struct tm *new_t; if (offset_min != 0) { /* FIXME: converting between time formats could make some data lost, better to operate over struct tm directly */ @@ -51,8 +50,7 @@ shift_time (struct tm *tm, int offset_min) t += offset_min * 60; - new_t = localtime (&t); - memcpy (tm, new_t, sizeof (struct tm)); + localtime_r (&t, tm); } } @@ -246,26 +244,31 @@ get_exif_data_fixed (ExifData *exif, const gchar *key) } catch (...) { } if (val && strlen (val) > 0) { - struct tm *tt; + struct tm *tt = NULL; char conv[1024]; + gchar *res = NULL; memset (&conv, 0, sizeof (conv)); if (exif->fake_datetime != (time_t) -1) { - tt = localtime (&exif->fake_datetime); + tt = (struct tm *) g_malloc0 (sizeof (struct tm)); + localtime_r (&exif->fake_datetime, tt); if (strftime (&conv[0], sizeof (conv), "%c", tt)) - return g_strdup (&conv[0]); + res = g_strdup (&conv[0]); } - tt = parse_exif_date (val); - if (tt) { - shift_time (tt, exif->timezone_shift); - if (strftime (&conv[0], sizeof (conv), "%c", tt)) { - g_free (tt); - return g_strdup (&conv[0]); + if (! res) { + tt = parse_exif_date (val); + if (tt) { + shift_time (tt, exif->timezone_shift); + if (strftime (&conv[0], sizeof (conv), "%c", tt)) + res = g_strdup (&conv[0]); } - g_free (tt); } + + g_free (tt); + if (res) + return res; } } @@ -579,7 +582,7 @@ shift_exif_time (Exiv2::ExifData& exifData, const char *key, int amount) static gboolean shift_iptc_time (Exiv2::IptcData &iptcData, int amount) { - struct tm *tt; + struct tm tt = {0}; long int orig_time; gboolean res; Exiv2::DateValue dval; @@ -592,13 +595,13 @@ shift_iptc_time (Exiv2::IptcData &iptcData, int amount) (iptc_has_key (iptcData, "Iptc.Application2.TimeCreated") ? iptcData["Iptc.Application2.TimeCreated"].toLong() : 0); if (orig_time > 0) { orig_time += amount * 60; - tt = localtime (&orig_time); - mktime (tt); - if (tt->tm_isdst) - shift_time (tt, -60); + localtime_r (&orig_time, &tt); + mktime (&tt); + if (tt.tm_isdst) + shift_time (&tt, -60); - dval = Exiv2::DateValue(tt->tm_year + 1900, tt->tm_mon + 1, tt->tm_mday); - tval = Exiv2::TimeValue(tt->tm_hour, tt->tm_min, tt->tm_sec, 0, 0); + dval = Exiv2::DateValue(tt.tm_year + 1900, tt.tm_mon + 1, tt.tm_mday); + tval = Exiv2::TimeValue(tt.tm_hour, tt.tm_min, tt.tm_sec, 0, 0); iptcData["Iptc.Application2.DateCreated"].setValue(&dval); iptcData["Iptc.Application2.TimeCreated"].setValue(&tval); res = TRUE; @@ -611,15 +614,15 @@ shift_iptc_time (Exiv2::IptcData &iptcData, int amount) static gboolean fake_exif_time (Exiv2::ExifData &exifData, const char *key, time_t datetime) { - struct tm *tt; + struct tm tt = {0}; gchar *st; gboolean res; res = FALSE; try { if (exifData[key].count() > 0) { - tt = localtime (&datetime); - st = format_exif_time (tt); + localtime_r (&datetime, &tt); + st = format_exif_time (&tt); if (st) exifData[key] = st; g_free (st); @@ -633,14 +636,14 @@ fake_exif_time (Exiv2::ExifData &exifData, const char *key, time_t datetime) static void fake_iptc_time (Exiv2::IptcData &iptcData, time_t datetime) { - struct tm *tt; + struct tm tt = {0}; Exiv2::DateValue dval; Exiv2::TimeValue tval; - tt = localtime (&datetime); + localtime_r (&datetime, &tt); - dval = Exiv2::DateValue(tt->tm_year + 1900, tt->tm_mon + 1, tt->tm_mday); - tval = Exiv2::TimeValue(tt->tm_hour, tt->tm_min, tt->tm_sec, 0, 0); + dval = Exiv2::DateValue(tt.tm_year + 1900, tt.tm_mon + 1, tt.tm_mday); + tval = Exiv2::TimeValue(tt.tm_hour, tt.tm_min, tt.tm_sec, 0, 0); iptcData["Iptc.Application2.DateCreated"].setValue(&dval); iptcData["Iptc.Application2.TimeCreated"].setValue(&tval); } |
