Skip to content

Commit

Permalink
[USDA] Do some property name validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
syoyo committed Oct 23, 2023
1 parent e1789d7 commit ce3d2df
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/ascii-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1420,10 +1420,12 @@ bool AsciiParser::ReadPrimAttrIdentifier(std::string *token) {
ss << c;
}

// ':' must lie in the middle of string literal
if (ss.str().back() == ':') {
PUSH_ERROR_AND_RETURN("PrimAttr name must not ends with `:`\n");
return false;
{
std::string name_err;
if (!pathutil::ValidatePropPath(Path("", ss.str()), &name_err)) {
PUSH_ERROR_AND_RETURN_TAG(kAscii,
fmt::format("Invalid Property name `{}`: {}", ss.str(), name_err));
}
}

// '.' must lie in the middle of string literal
Expand All @@ -1432,7 +1434,7 @@ bool AsciiParser::ReadPrimAttrIdentifier(std::string *token) {
return false;
}

// Currently we only support '.connect'

std::string tok = ss.str();

if (contains(tok, '.')) {
Expand Down Expand Up @@ -3140,6 +3142,15 @@ bool AsciiParser::ParseAttrMeta(AttrMeta *out_meta) {
fmt::format("Unsupported Property metadatum name: {}", varname));
}

{
std::string name_err;
if (!pathutil::ValidatePropPath(Path("", varname), &name_err)) {
PUSH_ERROR_AND_RETURN_TAG(kAscii,
fmt::format("Invalid Property name `{}`: {}", varname, name_err));
}
}


if (!SkipWhitespaceAndNewline()) {
return false;
}
Expand Down Expand Up @@ -3260,7 +3271,7 @@ bool AsciiParser::ParseAttrMeta(AttrMeta *out_meta) {
metavar.set_name(varname);

// add to custom meta
out_meta->meta.emplace(varname, metavar);
out_meta->meta[varname] = metavar;

} else {
// This should not happen though.
Expand Down
35 changes: 35 additions & 0 deletions src/path-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,40 @@ bool ResolveRelativePath(const Path &base_prim_path, const Path &relative_path,
return true;
}

bool ValidatePropPath(const Path &path, std::string *err) {
if (path.prop_part() == ":") {
if (err) {
(*err) = "Namespace delimiter only in Property path.";
}
return false;
}

if (startsWith(path.prop_part(), ":")) {
if (err) {
(*err) = "Property path starts with namespace delimiter.";
}
return false;
}

if (endsWith(path.prop_part(), ":")) {
if (err) {
(*err) = "Property path ends with namespace delimiter.";
}
return false;
}

if (contains_str(path.prop_part(), "::")) {
if (err) {
(*err) = "Empty path among namespace delimiters(`::`) in Property path.";
}
return false;
}

// TODO: more validation

return true;

}

} // namespace pathutil
} // namespace tinyusdz
5 changes: 5 additions & 0 deletions tests/usda/fail-case/empty-namespace-propname-000.usda
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#usda 1.0

def "bora" {
int bora::dora = 3
}
Binary file added tests/usdc/failure-case/issue-namespace-000.usdc
Binary file not shown.

0 comments on commit ce3d2df

Please sign in to comment.