Source: Avoid out-of-range inputs to std::isspace()

`isspace` takes `int` but documents that the value must be representable
by `unsigned char`, or be EOF.  Use a wrapper to cast to `unsigned char`
to avoid sign extension while converting to `int`.  This generalizes the
fix from commit 5e8c176e2a (cmExecuteProcessCommand: Cast c to unsigned
char before cast to int, 2024-01-05) to other `isspace` call sites.

This was detected by assertions in the MSVC standard library while
processing UTF-8 text.

Issue: #25561
This commit is contained in:
Brad King
2024-01-17 09:56:00 -05:00
parent 14abdc8e2b
commit d9d9326e14
8 changed files with 18 additions and 23 deletions
+1 -2
View File
@@ -6,7 +6,6 @@
#include "cmStringCommand.h"
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <limits>
@@ -660,7 +659,7 @@ bool HandleStripCommand(std::vector<std::string> const& args,
const char* ptr = stringValue.c_str();
size_t cc;
for (cc = 0; cc < inStringLength; ++cc) {
if (!isspace(*ptr)) {
if (!cmIsSpace(*ptr)) {
if (startPos > inStringLength) {
startPos = cc;
}