diff mbox series

[1/1] lib: Add missing checks for invalid return value

Message ID 20240711201306.98519-1-pvorel@suse.cz
State Accepted
Headers show
Series [1/1] lib: Add missing checks for invalid return value | expand

Commit Message

Petr Vorel July 11, 2024, 8:13 p.m. UTC
It's a common approach to test invalid return value in safe macros.

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
 include/lapi/landlock.h | 12 ++++++++++++
 lib/tst_safe_macros.c   |  4 ++++
 2 files changed, 16 insertions(+)

Comments

Li Wang July 12, 2024, 1:17 a.m. UTC | #1
On Fri, Jul 12, 2024 at 4:13 AM Petr Vorel <pvorel@suse.cz> wrote:

> It's a common approach to test invalid return value in safe macros.
>
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
>

Reviewed-by: Li Wang <liwang@redhat.com>

---
>  include/lapi/landlock.h | 12 ++++++++++++
>  lib/tst_safe_macros.c   |  4 ++++
>  2 files changed, 16 insertions(+)
>
> diff --git a/include/lapi/landlock.h b/include/lapi/landlock.h
> index 6d85eb12e3..d3fa760e5b 100644
> --- a/include/lapi/landlock.h
> +++ b/include/lapi/landlock.h
> @@ -133,6 +133,10 @@ static inline int safe_landlock_create_ruleset(const
> char *file, const int linen
>                 tst_brk_(file, lineno, TBROK | TERRNO,
>                         "landlock_create_ruleset(%p, %lu, %u)",
>                         attr, size, flags);
> +       } else if (rval < 0) {
> +               tst_brk_(file, lineno, TBROK | TERRNO,
> +                       "Invalid landlock_create_ruleset(%p, %lu, %u)
> return value %d",
> +                       attr, size, flags, rval);
>         }
>
>         return rval;
> @@ -151,6 +155,10 @@ static inline int safe_landlock_add_rule(const char
> *file, const int lineno,
>                 tst_brk_(file, lineno, TBROK | TERRNO,
>                         "landlock_add_rule(%d, %d, %p, %u)",
>                         ruleset_fd, rule_type, rule_attr, flags);
> +       } else if (rval < 0) {
> +               tst_brk_(file, lineno, TBROK | TERRNO,
> +                       "Invalid landlock_add_rule(%d, %d, %p, %u) return
> value %d",
> +                       ruleset_fd, rule_type, rule_attr, flags, rval);
>         }
>
>         return rval;
> @@ -166,6 +174,10 @@ static inline int safe_landlock_restrict_self(const
> char *file, const int lineno
>                 tst_brk_(file, lineno, TBROK | TERRNO,
>                         "landlock_restrict_self(%d, %u)",
>                         ruleset_fd, flags);
> +       } else if (rval < 0) {
> +               tst_brk_(file, lineno, TBROK | TERRNO,
> +                       "Invalid landlock_restrict_self(%d, %u) return
> value %d",
> +                       ruleset_fd, flags, rval);
>         }
>
>         return rval;
> diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c
> index 9301f3dd27..1bc5c92f58 100644
> --- a/lib/tst_safe_macros.c
> +++ b/lib/tst_safe_macros.c
> @@ -723,6 +723,10 @@ int safe_prctl(const char *file, const int lineno,
>                 tst_brk_(file, lineno, TBROK | TERRNO,
>                         "prctl(%d, %lu, %lu, %lu, %lu)",
>                         option, arg2, arg3, arg4, arg5);
> +       } else if (rval < 0) {
> +               tst_brk_(file, lineno, TBROK | TERRNO,
> +                       "Invalid prctl(%d, %lu, %lu, %lu, %lu) return
> value %d",
> +                       option, arg2, arg3, arg4, arg5, rval);
>         }
>
>         return rval;
> --
> 2.45.2
>
>
diff mbox series

Patch

diff --git a/include/lapi/landlock.h b/include/lapi/landlock.h
index 6d85eb12e3..d3fa760e5b 100644
--- a/include/lapi/landlock.h
+++ b/include/lapi/landlock.h
@@ -133,6 +133,10 @@  static inline int safe_landlock_create_ruleset(const char *file, const int linen
 		tst_brk_(file, lineno, TBROK | TERRNO,
 			"landlock_create_ruleset(%p, %lu, %u)",
 			attr, size, flags);
+	} else if (rval < 0) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid landlock_create_ruleset(%p, %lu, %u) return value %d",
+			attr, size, flags, rval);
 	}
 
 	return rval;
@@ -151,6 +155,10 @@  static inline int safe_landlock_add_rule(const char *file, const int lineno,
 		tst_brk_(file, lineno, TBROK | TERRNO,
 			"landlock_add_rule(%d, %d, %p, %u)",
 			ruleset_fd, rule_type, rule_attr, flags);
+	} else if (rval < 0) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid landlock_add_rule(%d, %d, %p, %u) return value %d",
+			ruleset_fd, rule_type, rule_attr, flags, rval);
 	}
 
 	return rval;
@@ -166,6 +174,10 @@  static inline int safe_landlock_restrict_self(const char *file, const int lineno
 		tst_brk_(file, lineno, TBROK | TERRNO,
 			"landlock_restrict_self(%d, %u)",
 			ruleset_fd, flags);
+	} else if (rval < 0) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid landlock_restrict_self(%d, %u) return value %d",
+			ruleset_fd, flags, rval);
 	}
 
 	return rval;
diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c
index 9301f3dd27..1bc5c92f58 100644
--- a/lib/tst_safe_macros.c
+++ b/lib/tst_safe_macros.c
@@ -723,6 +723,10 @@  int safe_prctl(const char *file, const int lineno,
 		tst_brk_(file, lineno, TBROK | TERRNO,
 			"prctl(%d, %lu, %lu, %lu, %lu)",
 			option, arg2, arg3, arg4, arg5);
+	} else if (rval < 0) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid prctl(%d, %lu, %lu, %lu, %lu) return value %d",
+			option, arg2, arg3, arg4, arg5, rval);
 	}
 
 	return rval;