From 470fbcd97de2101151ba4dec3f0fb806c23ef4c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B?= Date: Wed, 3 Jan 2024 22:13:26 +0800 Subject: [PATCH] Fix switch-case condition error in sample code (#245) The code 'case -EINPROGRESS || -EBUSY: ' is the same as 'case -115 || -16 :' at compiler time, as both error code are implemented with macro like '#define EBUSY 16'. The code above is essentially the same as 'case 1:'. In C, there is no real boolean value. Boolean-like value will be converted to 1 or 0. It does not matter too much if the '-EINPROGRESS || -EBUSY' is calculated at build time or at runtime. In both case, it will compare the 'rc' with 1 in the switch expression. It will not compare the 'rc' with any real error code number. When the code is really '-EBUSY', the execution will fallback to the default branch. And in practice, most of the compilers will do this simple compile-time static calculation, and generate code like static int test_skcipher_result(struct skcipher_def *sk, int rc) { switch (rc) { case 0: break; case 1: rc = wait_for_completion_interruptible(&sk->result.completion); /* code removed for conciseness */ break; default: pr_info("skcipher encrypt returned with %d result %d\n", rc, sk->result.err); break; } init_completion(&sk->result.completion); return rc; } --- examples/cryptosk.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/cryptosk.c b/examples/cryptosk.c index 7d4799a..c46e523 100644 --- a/examples/cryptosk.c +++ b/examples/cryptosk.c @@ -46,7 +46,8 @@ static int test_skcipher_result(struct skcipher_def *sk, int rc) switch (rc) { case 0: break; - case -EINPROGRESS || -EBUSY: + case -EINPROGRESS: + case -EBUSY: rc = wait_for_completion_interruptible(&sk->result.completion); if (!rc && !sk->result.err) { reinit_completion(&sk->result.completion);