Skip to content

Commit

Permalink
Change return of S_get_category_index_nowarn
Browse files Browse the repository at this point in the history
This is in preparation for it being called from more than a single
place.  Instead of returning -1 on error, it returns an out-of-bounds
value 1 greater than the largest legal value.  There is a placeholder
entry in all the arrays this can be indexed into for that illegal value.
This means if it somehow gets used, we wont get a segfault, like we
would on the -1, but instead a value that will show up as wrong, but not
crash.
  • Loading branch information
khwilliamson committed May 6, 2023
1 parent b1f3d4a commit 058b82e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion embed.fnc
Expand Up @@ -4328,7 +4328,7 @@ S |void |populate_hash_from_localeconv \
RST |unsigned int|get_category_index \
|const int category \
|NULLOK const char *locale
RST |int |get_category_index_nowarn \
RST |unsigned int|get_category_index_nowarn \
|const int category
Ri |const char *|mortalized_pv_copy \
|NULLOK const char * const pv
Expand Down
10 changes: 5 additions & 5 deletions locale.c
Expand Up @@ -532,7 +532,7 @@ S_get_displayable_string(pTHX_
#endif
#ifdef USE_LOCALE

STATIC int
STATIC unsigned int
S_get_category_index_nowarn(const int category)
{
/* Given a category, return the equivalent internal index we generally use
Expand All @@ -555,7 +555,8 @@ S_get_category_index_nowarn(const int category)
}
}

return -1;
/* Return an out-of-bounds value */
return LC_ALL_INDEX_ + 1;
}

STATIC unsigned int
Expand All @@ -570,7 +571,7 @@ S_get_category_index(const int category, const char * locale)
const char * conditional_warn_text = "; can't set it to ";
const int index = get_category_index_nowarn(category);

if (index >= 0) {
if (index <= LC_ALL_INDEX_) {
return index;
}

Expand All @@ -588,8 +589,7 @@ S_get_category_index(const int category, const char * locale)

SET_EINVAL;

/* Return an out-of-bounds value */
return LC_ALL_INDEX_ + 1;
return index; /* 'index' is known to be out-of-bounds */
}

#endif /* ifdef USE_LOCALE */
Expand Down
2 changes: 1 addition & 1 deletion proto.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 058b82e

Please sign in to comment.