Skip to content

Commit

Permalink
locale.c: Free curlocales[j] before overwriting
Browse files Browse the repository at this point in the history
The way to `curlocales[]` was handled in `Perl_init_i18nl10n' was a bit
fragile (in my opinion).

If one wasn't careful then it could lead to
- double free
- a (small) memory leak

With the current code there does not appear to be a way to trigger either
of those but it was to fragile for my liking.

By calling `Safefree` before overwriting the value the code becomes (imo) more
logical and the risk of the double free/memory leak disappears.

For much more details see GH #20002
  • Loading branch information
Bram authored and khwilliamson committed Jul 31, 2022
1 parent 503bf96 commit cf59ced
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions locale.c
Original file line number Diff line number Diff line change
Expand Up @@ -3541,6 +3541,7 @@ Perl_init_i18nl10n(pTHX_ int printwarn)
if (! setlocale_failure) {
unsigned int j;
for (j = 0; j < NOMINAL_LC_ALL_INDEX; j++) {
Safefree(curlocales[j]);
curlocales[j] = setlocale_i(j, trial_locale);
if (! curlocales[j]) {
setlocale_failure = TRUE;
Expand Down Expand Up @@ -3576,9 +3577,6 @@ Perl_init_i18nl10n(pTHX_ int printwarn)
if (! curlocales[j]) {
PerlIO_printf(Perl_error_log, "\t%s\n", category_names[j]);
}
else {
Safefree(curlocales[j]);
}
}

# endif /* LC_ALL */
Expand Down

0 comments on commit cf59ced

Please sign in to comment.