From dbfca1bdff2b4213a08cdc28e5503b254f2d20a0 Mon Sep 17 00:00:00 2001 From: George Rennie Date: Fri, 1 Nov 2024 16:57:10 +0100 Subject: [PATCH] frontends/ast.cc: special-case zero width strings as "\0" * Fixes #4696 --- frontends/ast/ast.cc | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/frontends/ast/ast.cc b/frontends/ast/ast.cc index 935273c9f..c5bf5b4ad 100644 --- a/frontends/ast/ast.cc +++ b/frontends/ast/ast.cc @@ -877,16 +877,25 @@ AstNode *AstNode::mkconst_str(const std::vector &v) // create an AST node for a constant (using a string as value) AstNode *AstNode::mkconst_str(const std::string &str) { - std::vector data; - data.reserve(str.size() * 8); - for (size_t i = 0; i < str.size(); i++) { - unsigned char ch = str[str.size() - i - 1]; - for (int j = 0; j < 8; j++) { - data.push_back((ch & 1) ? State::S1 : State::S0); - ch = ch >> 1; + AstNode *node; + + // LRM 1364-2005 5.2.3.3 The empty string literal ("") shall be considered + // equivalent to the ASCII NUL ("\0") + if (str.empty()) { + node = AstNode::mkconst_int(0, false, 8); + } else { + std::vector data; + data.reserve(str.size() * 8); + for (size_t i = 0; i < str.size(); i++) { + unsigned char ch = str[str.size() - i - 1]; + for (int j = 0; j < 8; j++) { + data.push_back((ch & 1) ? State::S1 : State::S0); + ch = ch >> 1; + } } + node = AstNode::mkconst_bits(data, false); } - AstNode *node = AstNode::mkconst_bits(data, false); + node->is_string = true; node->str = str; return node;