selftests/bpf: no need to track next_match_pos in struct test_loader
authorEduard Zingerman <eddyz87@gmail.com>
Mon, 22 Jul 2024 23:38:40 +0000 (16:38 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 29 Jul 2024 22:05:06 +0000 (15:05 -0700)
The call stack for validate_case() function looks as follows:
- test_loader__run_subtests()
  - process_subtest()
    - run_subtest()
      - prepare_case(), which does 'tester->next_match_pos = 0';
      - validate_case(), which increments tester->next_match_pos.

Hence, each subtest is run with next_match_pos freshly set to zero.
Meaning that there is no need to persist this variable in the
struct test_loader, use local variable instead.

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20240722233844.1406874-7-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
tools/testing/selftests/bpf/test_loader.c
tools/testing/selftests/bpf/test_progs.h

index f14e10b0de96e5a98a5662d0adfa541940cc2ee5..47508cf66e896baf5687034f1baa0de669b6a5bf 100644 (file)
@@ -434,7 +434,6 @@ static void prepare_case(struct test_loader *tester,
        bpf_program__set_flags(prog, prog_flags | spec->prog_flags);
 
        tester->log_buf[0] = '\0';
-       tester->next_match_pos = 0;
 }
 
 static void emit_verifier_log(const char *log_buf, bool force)
@@ -450,25 +449,23 @@ static void validate_case(struct test_loader *tester,
                          struct bpf_program *prog,
                          int load_err)
 {
-       int i, j, err;
-       char *match;
        regmatch_t reg_match[1];
+       const char *log = tester->log_buf;
+       int i, j, err;
 
        for (i = 0; i < subspec->expect_msg_cnt; i++) {
                struct expect_msg *msg = &subspec->expect_msgs[i];
+               const char *match = NULL;
 
                if (msg->substr) {
-                       match = strstr(tester->log_buf + tester->next_match_pos, msg->substr);
+                       match = strstr(log, msg->substr);
                        if (match)
-                               tester->next_match_pos = match - tester->log_buf + strlen(msg->substr);
+                               log += strlen(msg->substr);
                } else {
-                       err = regexec(&msg->regex,
-                                     tester->log_buf + tester->next_match_pos, 1, reg_match, 0);
+                       err = regexec(&msg->regex, log, 1, reg_match, 0);
                        if (err == 0) {
-                               match = tester->log_buf + tester->next_match_pos + reg_match[0].rm_so;
-                               tester->next_match_pos += reg_match[0].rm_eo;
-                       } else {
-                               match = NULL;
+                               match = log + reg_match[0].rm_so;
+                               log += reg_match[0].rm_eo;
                        }
                }
 
index 51341d50213b9dac024039257b9810055a9c8f98..b1e949fb16cf3378026a3ac6498940936e4025fa 100644 (file)
@@ -447,7 +447,6 @@ typedef int (*pre_execution_cb)(struct bpf_object *obj);
 struct test_loader {
        char *log_buf;
        size_t log_buf_sz;
-       size_t next_match_pos;
        pre_execution_cb pre_execution_cb;
 
        struct bpf_object *obj;