Skip to content

Commit

Permalink
vutil.c: Don't use perl macros that assume perl control
Browse files Browse the repository at this point in the history
Because 'version' can be called from anywhere anytime, it can't assume
that the macros that perl uses to toggle the locale for LC_NUMERIC
actually will work.  Until this commit, it was.  A file will be added
later that would faile without this patch.
  • Loading branch information
khwilliamson committed Sep 13, 2023
1 parent 7b25749 commit 7103e37
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions vutil.c
Expand Up @@ -7,6 +7,25 @@

#define VERSION_MAX 0x7FFFFFFF

#ifndef POSIX_SETLOCALE_LOCK
# ifdef gwLOCALE_LOCK
# define POSIX_SETLOCALE_LOCK gwLOCALE_LOCK
# define POSIX_SETLOCALE_UNLOCK gwLOCALE_UNLOCK
# else
# define POSIX_SETLOCALE_LOCK NOOP
# define POSIX_SETLOCALE_UNLOCK NOOP
# endif
#endif
#ifndef DISABLE_LC_NUMERIC_CHANGES
# ifdef LOCK_LC_NUMERIC_STANDARD
# define DISABLE_LC_NUMERIC_CHANGES() LOCK_LC_NUMERIC_STANDARD()
# define REENABLE_LC_NUMERIC_CHANGES() UNLOCK_LC_NUMERIC_STANDARD()
# else
# define DISABLE_LC_NUMERIC_CHANGES() NOOP
# define REENABLE_LC_NUMERIC_CHANGES() NOOP
# endif
#endif

/*
=for apidoc prescan_version
Expand Down Expand Up @@ -569,6 +588,9 @@ to force this SV to be interpreted as an "extended" version.
#define GET_NUMERIC_VERSION(ver, sv, tbuf, buf, len) \
STMT_START { \
\
/* Prevent callees from trying to change the locale */ \
DISABLE_LC_NUMERIC_CHANGES(); \
\
/* We earlier created 'sv' for very large version numbers, to rely \
* on the specialized algorithms SV code has built-in for such \
* values */ \
Expand All @@ -582,6 +604,7 @@ to force this SV to be interpreted as an "extended" version.
buf = tbuf; \
} \
\
REENABLE_LC_NUMERIC_CHANGES(); \
} STMT_END

SV *
Expand Down Expand Up @@ -659,7 +682,7 @@ Perl_upg_version(pTHX_ SV *ver, bool qv)
/* In windows, or not threaded, or not thread-safe, if it isn't C,
* set it to C. */

LC_NUMERIC_LOCK(0); /* Start critical section */
POSIX_SETLOCALE_LOCK; /* Start critical section */

locale_name_on_entry = setlocale(LC_NUMERIC, NULL);
if ( strNE(locale_name_on_entry, "C")
Expand All @@ -674,19 +697,14 @@ Perl_upg_version(pTHX_ SV *ver, bool qv)
locale_name_on_entry = NULL;
}

/* Prevent recursed calls from trying to change back */
LOCK_LC_NUMERIC_STANDARD();

GET_NUMERIC_VERSION(ver, sv, tbuf, buf, len);

UNLOCK_LC_NUMERIC_STANDARD();

if (locale_name_on_entry) {
setlocale(LC_NUMERIC, locale_name_on_entry);
Safefree(locale_name_on_entry);
}

LC_NUMERIC_UNLOCK; /* End critical section */
POSIX_SETLOCALE_UNLOCK; /* End critical section */
# endif

}
Expand Down

0 comments on commit 7103e37

Please sign in to comment.