[Python-checkins] cpython (3.5): improve type-safe of and prevent double-frees in get_locale_info (#28119)

benjamin.peterson python-checkins at python.org
Wed Sep 14 01:47:08 EDT 2016


https://hg.python.org/cpython/rev/a27cb132e140
changeset:   103784:a27cb132e140
branch:      3.5
parent:      103758:36550e4f9b4c
user:        Benjamin Peterson <benjamin at python.org>
date:        Tue Sep 13 22:43:45 2016 -0700
summary:
  improve type-safe of and prevent double-frees in get_locale_info (#28119)

files:
  Python/formatter_unicode.c |  26 +++++++++-----------------
  1 files changed, 9 insertions(+), 17 deletions(-)


diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c
--- a/Python/formatter_unicode.c
+++ b/Python/formatter_unicode.c
@@ -347,9 +347,11 @@
 /************************************************************************/
 
 /* Locale type codes. */
-#define LT_CURRENT_LOCALE 0
-#define LT_DEFAULT_LOCALE 1
-#define LT_NO_LOCALE 2
+enum LocaleType {
+    LT_CURRENT_LOCALE,
+    LT_DEFAULT_LOCALE,
+    LT_NO_LOCALE
+};
 
 /* Locale info needed for formatting integers and the part of floats
    before and including the decimal. Note that locales only support
@@ -663,7 +665,7 @@
    LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
    none if LT_NO_LOCALE. */
 static int
-get_locale_info(int type, LocaleInfo *locale_info)
+get_locale_info(enum LocaleType type, LocaleInfo *locale_info)
 {
     switch (type) {
     case LT_CURRENT_LOCALE: {
@@ -676,21 +678,16 @@
         locale_info->thousands_sep = PyUnicode_DecodeLocale(
                                          locale_data->thousands_sep,
                                          NULL);
-        if (locale_info->thousands_sep == NULL) {
-            Py_DECREF(locale_info->decimal_point);
+        if (locale_info->thousands_sep == NULL)
             return -1;
-        }
         locale_info->grouping = locale_data->grouping;
         break;
     }
     case LT_DEFAULT_LOCALE:
         locale_info->decimal_point = PyUnicode_FromOrdinal('.');
         locale_info->thousands_sep = PyUnicode_FromOrdinal(',');
-        if (!locale_info->decimal_point || !locale_info->thousands_sep) {
-            Py_XDECREF(locale_info->decimal_point);
-            Py_XDECREF(locale_info->thousands_sep);
+        if (!locale_info->decimal_point || !locale_info->thousands_sep)
             return -1;
-        }
         locale_info->grouping = "\3"; /* Group every 3 characters.  The
                                          (implicit) trailing 0 means repeat
                                          infinitely. */
@@ -698,15 +695,10 @@
     case LT_NO_LOCALE:
         locale_info->decimal_point = PyUnicode_FromOrdinal('.');
         locale_info->thousands_sep = PyUnicode_New(0, 0);
-        if (!locale_info->decimal_point || !locale_info->thousands_sep) {
-            Py_XDECREF(locale_info->decimal_point);
-            Py_XDECREF(locale_info->thousands_sep);
+        if (!locale_info->decimal_point || !locale_info->thousands_sep)
             return -1;
-        }
         locale_info->grouping = no_grouping;
         break;
-    default:
-        assert(0);
     }
     return 0;
 }

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list