From 6f5af581e8b95a059e47132baefaab7dc50e8882 Mon Sep 17 00:00:00 2001 From: Enyby Date: Thu, 3 Jan 2019 18:09:00 +0200 Subject: [PATCH] Fix string.gsub for invalid use '%' in replacement string Check code: ``` print(pcall(load('string.gsub("test", "%S", "A%")'))) print(pcall(load('string.gsub("test", "%S", "%A")'))) ``` --- src/core/org/luaj/vm2/lib/StringLib.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/core/org/luaj/vm2/lib/StringLib.java b/src/core/org/luaj/vm2/lib/StringLib.java index 4c4617a8..c87770c3 100644 --- a/src/core/org/luaj/vm2/lib/StringLib.java +++ b/src/core/org/luaj/vm2/lib/StringLib.java @@ -877,8 +877,14 @@ public class StringLib extends TwoArgFunction { lbuf.append( (byte) b ); } else { ++i; // skip ESC - b = (byte) news.luaByte( i ); + b = (byte)(i < l ? news.luaByte( i ) : 0); if ( !Character.isDigit( (char) b ) ) { + if (b != L_ESC) error( "invalid use of '" + (char)L_ESC + + "' in replacement string: after '" + (char)L_ESC + + "' must be '0'-'9' or '" + (char)L_ESC + + "', but found " + (i < l ? "symbol '" + (char)b + "' with code " + b + + " at pos " + (i + 1) : + "end of string")); lbuf.append( b ); } else if ( b == '0' ) { lbuf.append( s.substring( soff, e ) );