Skip to content

Commit

Permalink
Check that the referenced enum member exists
Browse files Browse the repository at this point in the history
  • Loading branch information
generalmimon committed Jun 29, 2022
1 parent 8913518 commit 8dcd1be
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions shared/src/main/scala/io/kaitai/struct/format/EnumSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.kaitai.struct.problems.KSYParseError

import scala.collection.mutable

case class EnumSpec(map: Map[Long, EnumValueSpec]) {
case class EnumSpec(path: List[String], map: Map[Long, EnumValueSpec]) extends YAMLPath {
var name = List[String]()

/**
Expand All @@ -24,7 +24,7 @@ object EnumSpec {
def fromYaml(src: Any, path: List[String]): EnumSpec = {
val srcMap = ParseUtils.asMap(src, path)
val memberNameMap = mutable.Map[String, Long]()
EnumSpec(srcMap.map { case (id, desc) =>
EnumSpec(path, srcMap.map { case (id, desc) =>
val idLong = ParseUtils.asLong(id, path)
val value = EnumValueSpec.fromYaml(desc, path ++ List(idLong.toString))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class FieldNotFoundError(val name: String, val curClass: ClassSpec)
extends NotFoundError(s"unable to access '$name' in ${curClass.nameAsStr} context")
class EnumNotFoundError(val name: String, val curClass: ClassSpec)
extends NotFoundError(s"unable to find enum '$name', searching from ${curClass.nameAsStr}")
class EnumMemberNotFoundError(val label: String, val enum: String, val enumDefPath: String)
extends NotFoundError(s"unable to find enum member '$enum::$label' (enum '$enum' defined at /$enumDefPath)")
class MethodNotFoundError(val name: String, val dataType: DataType)
extends NotFoundError(s"don't know how to call method '$name' of object type '$dataType'")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.kaitai.struct.translators
import io.kaitai.struct.datatype.DataType
import io.kaitai.struct.exprlang.Ast
import io.kaitai.struct.format.Identifier
import io.kaitai.struct.precompile.EnumMemberNotFoundError

/**
* Validates expressions usage of types (in typecasting operator,
Expand Down Expand Up @@ -33,8 +34,10 @@ class ExpressionValidator(val provider: TypeProvider)
provider.resolveEnum(inType, enumType.name)
validate(id)
case Ast.expr.EnumByLabel(enumType, label, inType) =>
provider.resolveEnum(inType, enumType.name)
// TODO: check that label belongs to that enum
val enumSpec = provider.resolveEnum(inType, enumType.name)
if (!enumSpec.map.values.exists(_.name == label.name)) {
throw new EnumMemberNotFoundError(label.name, enumType.name, enumSpec.path.mkString("/"))
}
case Ast.expr.Name(name: Ast.identifier) =>
if (name.name == Identifier.SIZEOF) {
CommonSizeOf.getByteSizeOfClassSpec(provider.nowClass)
Expand Down

0 comments on commit 8dcd1be

Please sign in to comment.