8371762: Incorrect use of checked_cast in Arguments::process_settings_file

Reviewed-by: dholmes, kbarrett
This commit is contained in:
Axel Boldt-Christmas 2026-01-15 05:58:18 +00:00
parent 499b588202
commit b6b337926d

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -64,7 +64,6 @@
#include "runtime/vm_version.hpp" #include "runtime/vm_version.hpp"
#include "services/management.hpp" #include "services/management.hpp"
#include "utilities/align.hpp" #include "utilities/align.hpp"
#include "utilities/checkedCast.hpp"
#include "utilities/debug.hpp" #include "utilities/debug.hpp"
#include "utilities/defaultStream.hpp" #include "utilities/defaultStream.hpp"
#include "utilities/macros.hpp" #include "utilities/macros.hpp"
@ -1207,16 +1206,22 @@ bool Arguments::process_settings_file(const char* file_name, bool should_exist,
} }
char token[1024]; char token[1024];
int pos = 0; size_t pos = 0;
bool in_white_space = true; bool in_white_space = true;
bool in_comment = false; bool in_comment = false;
bool in_quote = false; bool in_quote = false;
int quote_c = 0; char quote_c = 0;
bool result = true; bool result = true;
int c = getc(stream); int c_or_eof = getc(stream);
while(c != EOF && pos < (int)(sizeof(token)-1)) { while (c_or_eof != EOF && pos < (sizeof(token) - 1)) {
// We have checked the c_or_eof for EOF. getc should only ever return the
// EOF or an unsigned char converted to an int. We cast down to a char to
// avoid the char to int promotions we would otherwise do in the comparisons
// below (which would be incorrect if we ever compared to a non-ascii char),
// and the int to char conversions we would otherwise do in the assignments.
const char c = static_cast<char>(c_or_eof);
if (in_white_space) { if (in_white_space) {
if (in_comment) { if (in_comment) {
if (c == '\n') in_comment = false; if (c == '\n') in_comment = false;
@ -1224,7 +1229,7 @@ bool Arguments::process_settings_file(const char* file_name, bool should_exist,
if (c == '#') in_comment = true; if (c == '#') in_comment = true;
else if (!isspace((unsigned char) c)) { else if (!isspace((unsigned char) c)) {
in_white_space = false; in_white_space = false;
token[pos++] = checked_cast<char>(c); token[pos++] = c;
} }
} }
} else { } else {
@ -1244,10 +1249,10 @@ bool Arguments::process_settings_file(const char* file_name, bool should_exist,
} else if (in_quote && (c == quote_c)) { } else if (in_quote && (c == quote_c)) {
in_quote = false; in_quote = false;
} else { } else {
token[pos++] = checked_cast<char>(c); token[pos++] = c;
} }
} }
c = getc(stream); c_or_eof = getc(stream);
} }
if (pos > 0) { if (pos > 0) {
token[pos] = '\0'; token[pos] = '\0';