summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Rosdahl <joel.rosdahl@maxar.com>2022-12-21 15:15:41 +0100
committerJoel Rosdahl <joel@rosdahl.net>2022-12-23 10:56:43 +0100
commit7cf41457e07ae0c2f8a05f96ed7894c9ac707dde (patch)
treea3eeb982bc94b3db5c1ce13783f74d8f96dc0283
parentaec8c89972d5b2e24ff56ce41d513586d744cfa2 (diff)
downloadccache-7cf41457e07ae0c2f8a05f96ed7894c9ac707dde.tar.gz
fix: Don't crash in TextTable for a single heading row
-rw-r--r--src/util/TextTable.cpp3
-rw-r--r--unittest/test_util_TextTable.cpp6
2 files changed, 9 insertions, 0 deletions
diff --git a/src/util/TextTable.cpp b/src/util/TextTable.cpp
index 618f7b7b..53014aa4 100644
--- a/src/util/TextTable.cpp
+++ b/src/util/TextTable.cpp
@@ -32,6 +32,7 @@ TextTable::add_heading(const std::string& text)
Cell cell(text);
cell.m_heading = true;
m_rows.push_back({cell});
+ m_columns = std::max(m_columns, size_t(1));
}
void
@@ -86,6 +87,8 @@ TextTable::render() const
std::string result;
for (const auto& row : m_rows) {
+ ASSERT(column_widths.size() >= row.size());
+
std::string r;
bool first = true;
for (size_t i = 0; i < row.size(); ++i) {
diff --git a/unittest/test_util_TextTable.cpp b/unittest/test_util_TextTable.cpp
index 36240731..1a996c04 100644
--- a/unittest/test_util_TextTable.cpp
+++ b/unittest/test_util_TextTable.cpp
@@ -33,6 +33,12 @@ TEST_CASE("TextTable")
CHECK(table.render() == "");
}
+ SUBCASE("only a heading")
+ {
+ table.add_heading("heading");
+ CHECK(table.render() == "heading\n");
+ }
+
SUBCASE("1x1")
{
table.add_row({"a"});