Strip trailing CR from lines read in --flagfile. On POSIX platforms, `std::ifstream` in text mode does not translate `\r\n` to `\n`, leaving a trailing `'\r'` on every line read by `std::getline` in `ArgsList::ReadFromFlagfile`. That trailing `'\r'` breaks standalone boolean flags (`--bool_flag\r`), corrupts `std::string` and nested `--flagfile` values, prevents empty-value resets (`--flag=\r`), bypasses the `--` guard, and fails strict flag parsers that do not strip whitespace. Strip a single trailing `'\r'` via `absl::StripSuffix(line, "\r")` before `absl::StripLeadingAsciiWhitespace`, matching Windows text-mode `std::ifstream` behavior while preserving intentional trailing spaces on `std::string` flags. PiperOrigin-RevId: 1020200864
diff --git a/absl/flags/parse.cc b/absl/flags/parse.cc index 940bf56..25669f3 100644 --- a/absl/flags/parse.cc +++ b/absl/flags/parse.cc
@@ -213,7 +213,8 @@ while (std::getline(flag_file, line)) { line_number++; - absl::string_view stripped = absl::StripLeadingAsciiWhitespace(line); + absl::string_view stripped = + absl::StripLeadingAsciiWhitespace(absl::StripSuffix(line, "\r")); if (stripped.empty() || stripped[0] == '#') { // Comment or empty line; just ignore.
diff --git a/absl/flags/parse_test.cc b/absl/flags/parse_test.cc index 138cf6e..9687950 100644 --- a/absl/flags/parse_test.cc +++ b/absl/flags/parse_test.cc
@@ -203,16 +203,17 @@ // temporary directory location. This way we can test inclusion of one flagfile // from another flagfile. const char* GetFlagfileFlag(const std::vector<FlagfileData>& ffd, - std::string& flagfile_flag) { + std::string& flagfile_flag, + absl::string_view line_ending = "\n") { flagfile_flag = "--flagfile="; absl::string_view separator; for (const auto& flagfile_data : ffd) { std::string flagfile_name = absl::StrCat(GetTestTempDir(), flagfile_data.file_name); - std::ofstream flagfile_out(flagfile_name); + std::ofstream flagfile_out(flagfile_name, std::ios::binary); for (auto line : flagfile_data.file_lines) { - flagfile_out << absl::Substitute(line, GetTestTempDir()) << "\n"; + flagfile_out << absl::Substitute(line, GetTestTempDir()) << line_ending; } absl::StrAppend(&flagfile_flag, separator, flagfile_name); @@ -769,6 +770,57 @@ // -------------------------------------------------------------------- +TEST_F(ParseTest, TestFlagfileWithCrLf) { + std::string flagfile_flag; + + constexpr const char* const crlf_basic_data[] = { + "# comment", + "", + " ", + "--int_flag=42", + "--string_flag=plain", + "--bool_flag", + "--udt_flag=AAA", + }; + const char* in_args1[] = { + "testbin", + GetFlagfileFlag( + {{"parse_test.crlf_basic", absl::MakeConstSpan(crlf_basic_data)}}, + flagfile_flag, "\r\n"), + }; + TestParse(in_args1, 42, 1.1, "plain", true); + EXPECT_EQ(absl::GetFlag(FLAGS_udt_flag).value, 10); + + GetFlagfileFlag({{"parse_test.crlf_ff2", absl::MakeConstSpan(ff2_data)}, + {"parse_test.crlf_ff1", absl::MakeConstSpan(ff1_data)}}, + flagfile_flag, "\r\n"); + + constexpr const char* const crlf_include_data[] = { + "--flagfile=$0/parse_test.crlf_ff1", + "--flagfile=$0/parse_test.crlf_ff2", + }; + const char* in_args2[] = { + "testbin", + GetFlagfileFlag( + {{"parse_test.crlf_include", absl::MakeConstSpan(crlf_include_data)}}, + flagfile_flag, "\r\n"), + }; + TestParse(in_args2, 100, 0.1, "q2w2 ", false); + + constexpr const char* const crlf_empty_str_data[] = { + "--string_flag=", + }; + const char* in_args3[] = { + "testbin", + GetFlagfileFlag({{"parse_test.crlf_empty_str", + absl::MakeConstSpan(crlf_empty_str_data)}}, + flagfile_flag, "\r\n"), + }; + TestParse(in_args3, 100, 0.1, "", false); +} + +// -------------------------------------------------------------------- + TEST_F(ParseDeathTest, TestInvalidFlagfiles) { std::string flagfile_flag; @@ -808,6 +860,15 @@ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args3), "Flagfile can't contain position arguments or --"); + const char* in_args3_crlf[] = { + "testbin", + GetFlagfileFlag( + {{"parse_test.crlf_ff6", absl::MakeConstSpan(ff6_data)}}, + flagfile_flag, "\r\n"), + }; + EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args3_crlf), + "Flagfile can't contain position arguments or --"); + const char* in_args4[] = { "testbin", "--flagfile=invalid_flag_file",