Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59e5e0d

Browse files
committed
improve type-safe of and prevent double-frees in get_locale_info (#28119)
1 parent 1341926 commit 59e5e0d

1 file changed

Lines changed: 9 additions & 17 deletions

File tree

Python/formatter_unicode.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,11 @@ fill_padding(_PyUnicodeWriter *writer,
347347
/************************************************************************/
348348

349349
/* Locale type codes. */
350-
#define LT_CURRENT_LOCALE 0
351-
#define LT_DEFAULT_LOCALE 1
352-
#define LT_NO_LOCALE 2
350+
enum LocaleType {
351+
LT_CURRENT_LOCALE,
352+
LT_DEFAULT_LOCALE,
353+
LT_NO_LOCALE
354+
};
353355

354356
/* Locale info needed for formatting integers and the part of floats
355357
before and including the decimal. Note that locales only support
@@ -663,7 +665,7 @@ static char no_grouping[1] = {CHAR_MAX};
663665
LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
664666
none if LT_NO_LOCALE. */
665667
static int
666-
get_locale_info(int type, LocaleInfo *locale_info)
668+
get_locale_info(enum LocaleType type, LocaleInfo *locale_info)
667669
{
668670
switch (type) {
669671
case LT_CURRENT_LOCALE: {
@@ -676,37 +678,27 @@ get_locale_info(int type, LocaleInfo *locale_info)
676678
locale_info->thousands_sep = PyUnicode_DecodeLocale(
677679
locale_data->thousands_sep,
678680
NULL);
679-
if (locale_info->thousands_sep == NULL) {
680-
Py_DECREF(locale_info->decimal_point);
681+
if (locale_info->thousands_sep == NULL)
681682
return -1;
682-
}
683683
locale_info->grouping = locale_data->grouping;
684684
break;
685685
}
686686
case LT_DEFAULT_LOCALE:
687687
locale_info->decimal_point = PyUnicode_FromOrdinal('.');
688688
locale_info->thousands_sep = PyUnicode_FromOrdinal(',');
689-
if (!locale_info->decimal_point || !locale_info->thousands_sep) {
690-
Py_XDECREF(locale_info->decimal_point);
691-
Py_XDECREF(locale_info->thousands_sep);
689+
if (!locale_info->decimal_point || !locale_info->thousands_sep)
692690
return -1;
693-
}
694691
locale_info->grouping = "\3"; /* Group every 3 characters. The
695692
(implicit) trailing 0 means repeat
696693
infinitely. */
697694
break;
698695
case LT_NO_LOCALE:
699696
locale_info->decimal_point = PyUnicode_FromOrdinal('.');
700697
locale_info->thousands_sep = PyUnicode_New(0, 0);
701-
if (!locale_info->decimal_point || !locale_info->thousands_sep) {
702-
Py_XDECREF(locale_info->decimal_point);
703-
Py_XDECREF(locale_info->thousands_sep);
698+
if (!locale_info->decimal_point || !locale_info->thousands_sep)
704699
return -1;
705-
}
706700
locale_info->grouping = no_grouping;
707701
break;
708-
default:
709-
assert(0);
710702
}
711703
return 0;
712704
}

0 commit comments

Comments
 (0)