File 0445-binary-module-Always-detect-invalid-patterns.patch of Package erlang

From 2ee610d52b3695e43735481fb014905ee942116f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Thu, 31 Aug 2023 13:04:42 +0200
Subject: [PATCH] binary module: Always detect invalid patterns

Several functions in the `binary` module would accept an invalid
pattern (such as an atom) if the subject binary was empty or if
the `{scope,{0,0}}` option was given. The following functions were
affected:

    match/{2,3}
    matches/{2,3}
    replace/{3,4}
    split/{2,3}
---
 erts/emulator/beam/erl_bif_binary.c     |  8 --------
 lib/stdlib/test/binary_module_SUITE.erl | 12 ++++++++++++
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/erts/emulator/beam/erl_bif_binary.c b/erts/emulator/beam/erl_bif_binary.c
index a76f902a0f..cac5f8bab9 100644
--- a/erts/emulator/beam/erl_bif_binary.c
+++ b/erts/emulator/beam/erl_bif_binary.c
@@ -1535,10 +1535,6 @@ binary_match(Process *p, Eterm arg1, Eterm arg2, Eterm arg3, Uint flags)
     if (parse_match_opts_list(arg3, arg1, &(ctx->hsstart), &(ctx->hsend))) {
 	goto badarg;
     }
-    if (ctx->hsend == 0) {
-	result = do_match_not_found_result(p, arg1, &ctx);
-	BIF_RET(result);
-    }
     if (maybe_binary_match_compile(ctx, arg2, &pat_bin) != BF_OK) {
 	goto badarg;
     }
@@ -1597,10 +1593,6 @@ binary_split(Process *p, Eterm arg1, Eterm arg2, Eterm arg3)
     if (parse_split_opts_list(arg3, arg1, &(ctx->hsstart), &(ctx->hsend), &(ctx->flags))) {
 	goto badarg;
     }
-    if (ctx->hsend == 0) {
-	result = do_split_not_found_result(p, arg1, &ctx);
-	BIF_RET(result);
-    }
     if (maybe_binary_match_compile(ctx, arg2, &pat_bin) != BF_OK) {
 	goto badarg;
     }
diff --git a/lib/stdlib/test/binary_module_SUITE.erl b/lib/stdlib/test/binary_module_SUITE.erl
index 8127b93819..21593b4f49 100644
--- a/lib/stdlib/test/binary_module_SUITE.erl
+++ b/lib/stdlib/test/binary_module_SUITE.erl
@@ -78,6 +78,15 @@ badargs(Config) when is_list(Config) ->
 	   binary:match(<<1,2,3>>,<<1>>,
 			[{scope,{16#FFFFFFFFFFFFFFFF,
 				 16#7FFFFFFFFFFFFFFF}}])),
+    badarg = ?MASK_ERROR(binary:match(<<>>,foobar)),
+    badarg = ?MASK_ERROR(binary:match(<<"abc">>,foobar,
+                                      [{scope,{0,0}}])),
+    badarg = ?MASK_ERROR(binary:matches(<<>>,foobar)),
+    badarg = ?MASK_ERROR(binary:matches(<<"abc">>,foobar,
+                                        [{scope,{0,0}}])),
+    badarg = ?MASK_ERROR(binary:replace(<<>>,foobar,<<>>)),
+    badarg = ?MASK_ERROR(binary:replace(<<"abc">>,foobar,<<>>,
+                                        [{scope,{0,0}}])),
     badarg =
 	?MASK_ERROR(
 	   binary:part(<<1,2,3>>,{16#FF,
@@ -238,6 +247,9 @@ badargs(Config) when is_list(Config) ->
 	?MASK_ERROR(
 	   binary:at([1,2,4],2)),
 
+    badarg = ?MASK_ERROR(binary:split(<<>>,foobar)),
+    badarg = ?MASK_ERROR(binary:split(<<"abc">>,foobar,[{scope,{0,0}}])),
+
     badarg = ?MASK_ERROR(binary:encode_hex("abc")),
     badarg = ?MASK_ERROR(binary:encode_hex(123)),
     badarg = ?MASK_ERROR(binary:encode_hex([])),
-- 
2.35.3

openSUSE Build Service is sponsored by