Merge branch '1398-getdateformatw-error-handling' into 'master'

gdate: Add some missing error handling to GetDateFormatW() calls

Closes #1398

See merge request GNOME/glib!1529
This commit is contained in:
Sebastian Dröge 2020-06-17 06:57:23 +00:00
commit bd7d7ccdf5

View File

@ -2154,7 +2154,7 @@ g_date_order (GDate *date1,
} }
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
static void static gboolean
append_month_name (GArray *result, append_month_name (GArray *result,
LCID lcid, LCID lcid,
SYSTEMTIME *systemtime, SYSTEMTIME *systemtime,
@ -2169,9 +2169,14 @@ append_month_name (GArray *result,
{ {
base = abbreviated ? LOCALE_SABBREVMONTHNAME1 : LOCALE_SMONTHNAME1; base = abbreviated ? LOCALE_SABBREVMONTHNAME1 : LOCALE_SMONTHNAME1;
n = GetLocaleInfoW (lcid, base + systemtime->wMonth - 1, NULL, 0); n = GetLocaleInfoW (lcid, base + systemtime->wMonth - 1, NULL, 0);
if (n == 0)
return FALSE;
g_array_set_size (result, result->len + n); g_array_set_size (result, result->len + n);
GetLocaleInfoW (lcid, base + systemtime->wMonth - 1, if (GetLocaleInfoW (lcid, base + systemtime->wMonth - 1,
((wchar_t *) result->data) + result->len - n, n); ((wchar_t *) result->data) + result->len - n, n) != n)
return FALSE;
g_array_set_size (result, result->len - 1); g_array_set_size (result, result->len - 1);
} }
else else
@ -2179,12 +2184,20 @@ append_month_name (GArray *result,
/* According to MSDN, this is the correct method to obtain /* According to MSDN, this is the correct method to obtain
* the form of the month name used when formatting a full * the form of the month name used when formatting a full
* date; it must be a genitive case in some languages. * date; it must be a genitive case in some languages.
*
* (n == 0) indicates an error, whereas (n < 2) is something wed never
* expect from the given format string, and would break the subsequent code.
*/ */
lpFormat = abbreviated ? L"ddMMM" : L"ddMMMM"; lpFormat = abbreviated ? L"ddMMM" : L"ddMMMM";
n = GetDateFormatW (lcid, 0, systemtime, lpFormat, NULL, 0); n = GetDateFormatW (lcid, 0, systemtime, lpFormat, NULL, 0);
if (n < 2)
return FALSE;
g_array_set_size (result, result->len + n); g_array_set_size (result, result->len + n);
GetDateFormatW (lcid, 0, systemtime, lpFormat, if (GetDateFormatW (lcid, 0, systemtime, lpFormat,
((wchar_t *) result->data) + result->len - n, n); ((wchar_t *) result->data) + result->len - n, n) != n)
return FALSE;
/* We have obtained a day number as two digits and the month name. /* We have obtained a day number as two digits and the month name.
* Now let's get rid of those two digits: overwrite them with the * Now let's get rid of those two digits: overwrite them with the
* month name. * month name.
@ -2194,6 +2207,8 @@ append_month_name (GArray *result,
(n - 2) * sizeof (wchar_t)); (n - 2) * sizeof (wchar_t));
g_array_set_size (result, result->len - 3); g_array_set_size (result, result->len - 3);
} }
return TRUE;
} }
static gsize static gsize
@ -2286,12 +2301,16 @@ win32_strftime_helper (const GDate *d,
break; break;
case 'b': case 'b':
case 'h': case 'h':
append_month_name (result, lcid, &systemtime, TRUE, if (!append_month_name (result, lcid, &systemtime, TRUE, modifier == 'O'))
modifier == 'O'); {
/* Ignore the error; this placeholder will be replaced with nothing */
}
break; break;
case 'B': case 'B':
append_month_name (result, lcid, &systemtime, FALSE, if (!append_month_name (result, lcid, &systemtime, FALSE, modifier == 'O'))
modifier == 'O'); {
/* Ignore the error; this placeholder will be replaced with nothing */
}
break; break;
case 'c': case 'c':
n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0); n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);