character escaping changed
diff --git a/bpf-translate.cxx b/bpf-translate.cxx index bd03158..898d3ec 100644 --- a/bpf-translate.cxx +++ b/bpf-translate.cxx
@@ -920,7 +920,6 @@ } else if (in_string) { - // resulting string will be processed by translate_escapes() if (c == '"') arg.push_back(c), in_string = false; // include quote else if (c == '\\' && c2 == '"') @@ -1094,9 +1093,6 @@ return pos; } -/* forward declaration */ -std::string translate_escapes (const interned_string &str, const token* tok); - /* Convert a <reg> or <imm> operand to a value. May emit code to store a string constant on the stack. */ value * @@ -1171,8 +1167,7 @@ throw SEMANTIC_ERROR (_F("BUG: improper string %s", arg.c_str()), stmt.tok); std::string escaped_str = arg.substr(1,arg.size()-2); /* strip quotes */ - std::string str = translate_escapes(escaped_str, stmt.tok); - return emit_literal_string(str, stmt.tok); + return emit_literal_string(escaped_str, stmt.tok); } else if (arg == "BPF_MAXSTRINGLEN" || arg == "BPF_F_CURRENT_CPU") { @@ -1528,7 +1523,6 @@ "but first parameter is not a string literal", func_name.c_str()), stmt.tok); format = format.substr(1,format.size()-2); /* strip quotes */ - format = translate_escapes(format, stmt.tok); size_t format_bytes = format.size() + 1; if (format_bytes > BPF_MAXFORMATLEN) @@ -1995,99 +1989,6 @@ throw SEMANTIC_ERROR (_("unknown lvalue"), e->tok); } -// Translate string escape characters. -// Accepts strings produced by parse.cxx lexer::scan and -// by the eBPF embedded-code assembler. -// -// PR23559: This is currently an eBPF-only version of the function -// that does not translate octal escapes. -std::string -translate_escapes (const interned_string &str, const token* tok) -{ - std::string result; - bool saw_esc = false; - for (interned_string::const_iterator j = str.begin(); - j != str.end(); ++j) - { - if (saw_esc) - { - saw_esc = false; - switch (*j) - { - case 'f': result += '\f'; break; - case 'n': result += '\n'; break; - case 'r': result += '\r'; break; - case 't': result += '\t'; break; - case 'v': result += '\v'; break; - - // Translate octal and hex escapes: - case '0' ... '7': - { - unsigned int c = 0; - // An octal escape sequence is at most 3 characters. - for (unsigned k = 0; k < 3; ++k) - { - c = c * 8 + (*j - '0'); - ++j; - if (j == str.end() || *j < '0' || *j > '7') - { - --j; // avoid swallowing extra char - break; - } - } - - // TODO: this check should be performed by the parser - if (c > 255 /* \377 */) - throw SEMANTIC_ERROR (_("octal escape sequence out of range"), tok); - - if (c != 0) // XXX skip '\0' as it can break a transport tag - result += (char) c; - } - break; - case 'x': - { - unsigned int c = 0; ++j; - // A hex escape sequence is arbitrarily long. - // XXX: Behaviour is 'undefined' when overflowing char. - for (; j != str.end(); ++j) - { - if (*j >= '0' && *j <= '9') - c = c * 16 + (*j - '0'); - else if (*j >= 'a' && *j <= 'f') - c = c * 16 + (*j - 'a' + 10); - else if (*j >= 'A' && *j <= 'F') - c = c * 16 + (*j - 'A' + 10); - else - { - --j; // avoid swallowing extra char - break; - } - } - - // TODO: this check should be performed by the parser - if (c > 0xff) - throw SEMANTIC_ERROR (_("hex escape sequence out of range"), tok); - - if (c != 0) // XXX skip '\0' as it can break a transport tag - result += (char) c; - - // If we've hit the end of the string , then return the result. - if (j == str.end()) - return result; - } - break; - - default: result += *j; break; - } - } - else if (*j == '\\') - saw_esc = true; - else - result += *j; - } - return result; -} - value * bpf_unparser::emit_literal_string (const std::string &str, const token *tok) { @@ -2101,8 +2002,7 @@ bpf_unparser::visit_literal_string (literal_string* e) { interned_string v = e->value; - std::string str = translate_escapes(v, e->tok); - result = emit_literal_string(str, e->tok); + result = emit_literal_string(v.to_string(), e->tok); } void @@ -3412,7 +3312,7 @@ // If this is a long string with no actual arguments, it will be // interned in the format string table as usual. interned_string fstr = e->raw_components; - format += translate_escapes(fstr, e->tok); + format += fstr.to_string(); } else {
diff --git a/parse.cxx b/parse.cxx index e10906e..e37acd0 100644 --- a/parse.cxx +++ b/parse.cxx
@@ -1707,26 +1707,94 @@ c = input_get(); switch (c) { - case 'x': - if (!has_version("2.3")) - goto the_default; - /* FALLTHROUGH */ - case 'a': - case 'b': - case 't': - case 'n': - case 'v': - case 'f': - case 'r': - case '0' ... '7': // NB: need only match the first digit - case '\\': - // Pass these escapes through to the string value - // being parsed; it will be emitted into a C literal. - // XXX: PR13371: perhaps we should evaluate them here - // (and re-quote them during translate.cxx emission). - token_str.push_back ('\\'); + case 'x': // hex sequence + { + if (!has_version("2.3")) + goto the_default; - // fall through + // used for error message + n->location.line = cursor_line; + n->location.column = cursor_column; + + unsigned number = 0; + bool exists = false; + + while (true) + { + int next = input_peek(0); + + if (next >= '0' && next <= '9') + number = number * 16 + (next - '0'); + else if (next >= 'A' && next <= 'F') + number = number * 16 + (next - 'A' + 10); + else if (next >= 'a' && next <= 'f') + number = number * 16 + (next - 'a' + 10); + else + break; + + c = input_get(); // consume character + exists = true; // there's atleast one digit + + if (number > 0xff) + throw PARSE_ERROR (_("hex sequence out of range"), n); + } + + if (!exists) + throw PARSE_ERROR (_("\\x used with no following hex digits"), n); + else + token_str.push_back((unsigned char) number); + } + break; + case 'a': + token_str.push_back('\a'); + break; + case 'b': + token_str.push_back('\b'); + break; + case 't': + token_str.push_back('\t'); + break; + case 'n': + token_str.push_back('\n'); + break; + case 'v': + token_str.push_back('\v'); + break; + case 'f': + token_str.push_back('\f'); + break; + case 'r': + token_str.push_back('\r'); + break; + case '0' ... '7': // octal sequence + { + // used for error message + n->location.line = cursor_line; + n->location.column = cursor_column - 1; + + unsigned number = c - '0'; + + for (unsigned i = 0; i < 2; ++i) + { + int next = input_peek(0); + + if (next >= '0' && next <= '7') + number = number * 8 + (next - '0'); + else + break; + + c = input_get(); // consume character + } + + if (number > 0xff /* \377 */) + throw PARSE_ERROR (_("octal sequence out of range"), n); + else + token_str.push_back((unsigned char) number); + } + break; + case '\\': + token_str.push_back ('\\'); + break; default: the_default: token_str.push_back (c); break;
diff --git a/stapbpf/bpfinterp.cxx b/stapbpf/bpfinterp.cxx index ffdbdd1..940dcd2 100644 --- a/stapbpf/bpfinterp.cxx +++ b/stapbpf/bpfinterp.cxx
@@ -414,9 +414,9 @@ { stream << '\\'; switch (c) { - case '\0': stream << '0'; break; // Not handled by translate_escapes - case '\a': stream << 'a'; break; // Not handled by translate_escapes - case '\b': stream << 'b'; break; // Not handled by translate_escapes + case '\0': stream << '0'; break; + case '\a': stream << 'a'; break; + case '\b': stream << 'b'; break; case '\f': stream << 'f'; break; case '\n': stream << 'n'; break; case '\r': stream << 'r'; break;
diff --git a/staptree.cxx b/staptree.cxx index 5ae0283..1b7fa12 100644 --- a/staptree.cxx +++ b/staptree.cxx
@@ -815,11 +815,12 @@ j != i->literal_string.end(); ++j) { // See also: c_unparser::visit_literal_string and lex_cast_qstring - if (*j == '%') - oss << '%'; - else if(*j == '"') - oss << '\\'; - oss << *j; + if (!isprint(*j) || *j == '\\' || *j == '"') + oss << "\\x" << "0123456789ABCEF"[(*j & 0xf0) >> 4] << "0123456789ABCEF"[(*j & 0x0f) >> 0]; + else if (*j == '%') + oss << '%' << *j; + else + oss << *j; } } else
diff --git a/tapsets.cxx b/tapsets.cxx index 193d36f..31a3cf1 100644 --- a/tapsets.cxx +++ b/tapsets.cxx
@@ -8481,8 +8481,11 @@ } // PR13338: unquote glob results + //std::cout << "Path (1): " << module_name << std::endl; module_name = unescape_glob_chars (module_name); + //std::cout << "Path (2): " << module_name << std::endl; user_path = find_executable (module_name, sess.sysroot, sess.sysenv); // canonicalize it + //std::cout << "Path (3): " << user_path << std::endl; // Note we don't need to pass the sysroot to // is_fully_resolved(), since we just passed it to // find_executable().
diff --git a/translate.cxx b/translate.cxx index f5a73ec..3f5845e 100644 --- a/translate.cxx +++ b/translate.cxx
@@ -4663,17 +4663,17 @@ { interned_string v = e->value; o->line() << '"'; + for (unsigned i=0; i<v.size(); i++) - // NB: The backslash character is specifically passed through as is. - // This is because our parser treats "\" as an ordinary character, not - // an escape sequence, leaving it to the C compiler (and this function) - // to treat it as such. If we were to escape it, there would be no way - // of generating C-level escapes from script code. // See also print_format::components_to_string and lex_cast_qstring - if (v[i] == '"') // or other escapeworthy characters? - o->line() << '\\' << '"'; - else - o->line() << v[i]; + { + if (!isprint(v[i]) || v[i] == '"' || v[i] == '\\') + o->line() << "\\x" << "0123456789ABCEF"[(v[i] & 0xf0) >> 4] << "0123456789ABCEF"[(v[i] & 0x0f) >> 0]; + else + o->line() << v[i]; + + } + o->line() << '"'; }
diff --git a/util.cxx b/util.cxx index 0d898e6..2150c81 100644 --- a/util.cxx +++ b/util.cxx
@@ -609,6 +609,18 @@ const map<string, string>& sysenv, const string& env_path) { + /* TODO: + bool b1 = !path.empty(); + bool b2 = !contains_glob_chars(path); + bool b3 = path.find('/') != string::npos; + string s1 = path; + string s2 = find_executable(path, sysroot, sysenv, env_path); + bool b4 = s1 == s2; + + std::cout << "Variables: " << b1 << "-" << b2 << "-" << b3 << "-" << b4 << std::endl; + std::cout << "Original Path: " << s1 << " - New Path: " << s2 << std::endl; + */ + return !path.empty() && !contains_glob_chars(path) && path.find('/') != string::npos