Skip to content

Commit f4a224e

Browse files
thanmianlancetaylor
authored andcommitted
compiler: fix parsing issue with non-ASCII first package char
Fix a bug in the parser code that decides whether a given name should be considered exported or not. The function Lex::is_exported_name (which assumes that its input is a mangled name) was being called on non-mangled (raw utf-8) names in various places. For the bug in question this caused an imported package to be registered under the wrong name. To fix the issue, rename 'Lex::is_exported_name' to 'Lex::is_exported_mangled_name', and add a new 'Lex::is_exported_name' that works on utf-8 strings. Fixes golang/go#27836. Change-Id: I2d3b923c9f507348a7f0f2c8161d3cc2dc68d02f Reviewed-on: https://go-review.googlesource.com/137736 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 944784a commit f4a224e

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

go/import.cc

-2
Original file line numberDiff line numberDiff line change
@@ -983,8 +983,6 @@ Import::read_name()
983983
std::string ret = this->read_identifier();
984984
if (ret == "?")
985985
ret.clear();
986-
else if (!Lex::is_exported_name(ret))
987-
ret = '.' + this->package_->pkgpath() + '.' + ret;
988986
return ret;
989987
}
990988

go/lex.cc

+13-1
Original file line numberDiff line numberDiff line change
@@ -2764,7 +2764,7 @@ Lex::is_unicode_uppercase(unsigned int c)
27642764
// mangled name which includes only ASCII characters.
27652765

27662766
bool
2767-
Lex::is_exported_name(const std::string& name)
2767+
Lex::is_exported_mangled_name(const std::string& name)
27682768
{
27692769
unsigned char c = name[0];
27702770
if (c != '.')
@@ -2791,6 +2791,18 @@ Lex::is_exported_name(const std::string& name)
27912791
}
27922792
}
27932793

2794+
// Return whether the identifier NAME should be exported. NAME is a
2795+
// an unmangled utf-8 string and may contain non-ASCII characters.
2796+
2797+
bool
2798+
Lex::is_exported_name(const std::string& name)
2799+
{
2800+
unsigned int uchar;
2801+
if (Lex::fetch_char(name.c_str(), &uchar) != 0)
2802+
return Lex::is_unicode_letter(uchar) && Lex::is_unicode_uppercase(uchar);
2803+
return false;
2804+
}
2805+
27942806
// Return whether the identifier NAME contains an invalid character.
27952807
// This is based on how we handle invalid characters in
27962808
// gather_identifier.

go/lex.h

+5
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ class Lex
408408
// Return whether the identifier NAME should be exported. NAME is a
409409
// mangled name which includes only ASCII characters.
410410
static bool
411+
is_exported_mangled_name(const std::string& name);
412+
413+
// Return whether the identifier NAME should be exported. NAME is
414+
// an unmangled utf-8 string and may contain non-ASCII characters.
415+
static bool
411416
is_exported_name(const std::string& name);
412417

413418
// Return whether the identifier NAME is invalid. When we see an

0 commit comments

Comments
 (0)