Skip to content

Commit 6ea05d9

Browse files
committed
Use Java regex to validate HPy ABI tag
1 parent 892fc36 commit 6ea05d9

File tree

1 file changed

+14
-18
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy

1 file changed

+14
-18
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContext.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import java.util.concurrent.TimeUnit;
5757
import java.util.concurrent.atomic.AtomicReference;
5858
import java.util.logging.Level;
59+
import java.util.regex.Matcher;
60+
import java.util.regex.Pattern;
5961

6062
import com.oracle.graal.python.PythonLanguage;
6163
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
@@ -131,6 +133,9 @@ public final class GraalHPyContext extends CExtContext {
131133
public static final long SIZEOF_LONG = java.lang.Long.BYTES;
132134
private static final long NATIVE_ARGUMENT_STACK_SIZE = 1 << 15; // 32 kB stack size
133135

136+
// "blah.hpy123[-310].so"
137+
private static final Pattern SO_NAME_PATTERN = Pattern.compile(".*" + Pattern.quote(HPY_EXT) + "(\\d+)(?:-\\d+)?\\.so$");
138+
134139
@TruffleBoundary
135140
public static GraalHPyContext ensureHPyWasLoaded(Node node, PythonContext context, TruffleString name, TruffleString path) throws IOException, ApiInitException, ImportException {
136141
if (!context.hasHPyContext()) {
@@ -237,25 +242,16 @@ public static Object loadHPyModule(Node location, PythonContext context, Truffle
237242

238243
private static void validateABITag(Node location, String shortname, String soname, HPyABIVersion abiVersion) {
239244
// assumes format: "blah.hpy123[-310].so"
240-
int hpyExtIdx = soname.lastIndexOf(HPY_EXT);
241-
int start = hpyExtIdx + HPY_EXT.length();
242-
int end = start;
243-
while (Character.isDigit(soname.charAt(end))) {
244-
end++;
245-
}
246-
if (hpyExtIdx != -1 && end > start) {
247-
try {
248-
String abiTagVersion = soname.substring(start, end);
249-
int abiTag = Integer.parseInt(abiTagVersion);
250-
if (abiTag != abiVersion.major) {
251-
throw PRaiseNode.raiseUncached(location, PythonBuiltinClassType.RuntimeError, ErrorMessages.HPY_ABI_TAG_MISMATCH,
252-
shortname, soname, abiTag, abiVersion.major, abiVersion.minor);
253-
}
254-
// major version fits -> validation successful
255-
return;
256-
} catch (NumberFormatException e) {
257-
// fall through
245+
Matcher matcher = SO_NAME_PATTERN.matcher(soname);
246+
if (matcher.matches()) {
247+
String abiTagVersion = matcher.group(1);
248+
int abiTag = Integer.parseInt(abiTagVersion);
249+
if (abiTag != abiVersion.major) {
250+
throw PRaiseNode.raiseUncached(location, PythonBuiltinClassType.RuntimeError, ErrorMessages.HPY_ABI_TAG_MISMATCH,
251+
shortname, soname, abiTag, abiVersion.major, abiVersion.minor);
258252
}
253+
// major version fits -> validation successful
254+
return;
259255
}
260256
throw PRaiseNode.raiseUncached(location, PythonBuiltinClassType.RuntimeError, ErrorMessages.HPY_NO_ABI_TAG,
261257
shortname, soname, abiVersion.major, abiVersion.minor);

0 commit comments

Comments
 (0)