Copilot commented on code in PR #3889:
URL: https://github.com/apache/avro/pull/3889#discussion_r3612767907
##########
lang/c++/impl/Compiler.cc:
##########
@@ -132,6 +132,24 @@ string getStringField(const Entity &e, const Object &m,
return it->second.stringValue();
}
+// Validates that a record field name or enum symbol conforms to the Avro name
+// grammar (a non-empty sequence of [A-Za-z0-9_], as also enforced for named
type
+// simple names by Name::check()). This prevents out-of-spec strings from being
+// emitted verbatim as identifiers by the C++ code generator. The character
checks
+// are restricted to ASCII (rather than the locale-dependent std::isalnum),
which
+// is consistent with the locale-independent checks used by Name::check().
+static void validateSimpleName(const string &name, const char *what) {
+ if (name.empty()) {
+ throw Exception("Empty {} name", what);
+ }
+ for (const char c : name) {
+ const bool isAsciiAlnum = (c >= '0' && c <= '9') || (c >= 'A' && c <=
'Z') || (c >= 'a' && c <= 'z');
+ if (!isAsciiAlnum && c != '_') {
+ throw Exception("Invalid {} name: {}", what, name);
+ }
+ }
+}
Review Comment:
validateSimpleName() currently allows names starting with a digit (e.g.
"1abc"). Those names will still be emitted as C++ identifiers by avrogencpp
(decorate() only handles reserved words), so schema compilation can still
succeed while code generation produces uncompilable C++. Consider enforcing the
usual identifier rule that the first character must be [A-Za-z_] (and updating
the comment accordingly).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]