Skip to content

Commit

Permalink
Fix #354: Improve length check for string literal properties (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
glx22 authored Jan 6, 2025
1 parent a31f715 commit f97de8b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions nml/actions/action0.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,13 +611,13 @@ def apply_threshold(value):
if "string_literal" in prop_info and (
isinstance(value, expression.StringLiteral) or prop_info["string_literal"] != 4
):
# Parse non-string exprssions just like integers. User will have to take care of proper value.
# Parse non-string expressions just like integers. User will have to take care of proper value.
# This can be used to set a label (=string of length 4) to the value of a parameter.
if not isinstance(value, expression.StringLiteral):
raise generic.ScriptError(
"Value for property {:d} must be a string literal".format(prop_info["num"]), value.pos
)
if len(value.value) != prop_info["string_literal"]:
if grfstrings.get_string_size(value.value, False, True) != prop_info["string_literal"]:
raise generic.ScriptError(
"Value for property {:d} must be of length {:d}".format(
prop_info["num"], prop_info["string_literal"]
Expand Down
4 changes: 2 additions & 2 deletions nml/expression/string_literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
with NML; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""

from nml import generic
from nml import generic, grfstrings

from .base_expression import Expression, Type

Expand All @@ -37,7 +37,7 @@ def __str__(self):
return '"{}"'.format(self.value)

def write(self, file, size):
assert len(self.value) == size
assert grfstrings.get_string_size(self.value, False, True) == size
file.print_string(self.value, final_zero=False, force_ascii=True)

def reduce(self, id_dicts=None, unknown_id_fatal=True):
Expand Down

0 comments on commit f97de8b

Please sign in to comment.