linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tools-powerpc: Return false instead of -1
@ 2015-09-17 11:03 Peter Senna Tschudin
       [not found] ` <1442487801-22661-1-git-send-email-peter.senna-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Senna Tschudin @ 2015-09-17 11:03 UTC (permalink / raw)
  To: shuahkh-JPH+aEBZ4P+UEJcrhfAQsw, mpe-Gsx/Oe8HsFggBc27wqDAHg,
	sam.bobroff-8fk3Idey6ehBDgjK7y7TUQ,
	linux-api-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA
  Cc: Peter Senna Tschudin

Returning a negative value for a boolean function seem to have the
undesired effect of returning true. require_paranoia_below() is a
boolean function, but the variable used to store the return value is an
integer, receiving -1 or 0. This patch convert rc to bool, replace -1
by false, and 0 by true.

This issue was found by the following Coccinelle semantic patch:
<smpl>
@@
identifier f, ret;
constant C;
typedef bool;
@@
bool f (...){
<+...
ret = -C;
...
* return ret;
...+>
}
</smpl>

Signed-off-by: Peter Senna Tschudin <peter.senna-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
I could not compile this specific file, but I did some testing on toy examples:

t.c:
_Bool main(void)
{
        return -1;
}

$ powerpc64-linux-gnu-gcc -O2 -S -fomit-frame-pointer t.c

t.s:
...
.L.main:
        li 3,1
        blr

However if the function type is int instead of bool, it behaves as
expected:

t2.c:
int main(void)
{
        return -1;
}

$ powerpc64-linux-gnu-gcc -O2 -S -fomit-frame-pointer t2.c

t2.s:
...
.L.main:
        li 3,-1
        blr

So another solution would be to change the function type to int.

 tools/testing/selftests/powerpc/pmu/lib.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/powerpc/pmu/lib.c b/tools/testing/selftests/powerpc/pmu/lib.c
index a07104c..2f17110 100644
--- a/tools/testing/selftests/powerpc/pmu/lib.c
+++ b/tools/testing/selftests/powerpc/pmu/lib.c
@@ -219,9 +219,9 @@ bool require_paranoia_below(int level)
 	unsigned long current;
 	char *end, buf[16];
 	FILE *f;
-	int rc;
+	bool rc;
 
-	rc = -1;
+	rc = false;
 
 	f = fopen(PARANOID_PATH, "r");
 	if (!f) {
@@ -244,7 +244,7 @@ bool require_paranoia_below(int level)
 	if (current >= level)
 		goto out;
 
-	rc = 0;
+	rc = true;
 out_close:
 	fclose(f);
 out:
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: tools-powerpc: Return false instead of -1
       [not found] ` <1442487801-22661-1-git-send-email-peter.senna-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
@ 2016-09-02  9:54   ` Andrew Shadura
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Shadura @ 2016-09-02  9:54 UTC (permalink / raw)
  To: Peter Senna Tschudin, linux-api-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA
  Cc: shuahkh-JPH+aEBZ4P+UEJcrhfAQsw, mpe-Gsx/Oe8HsFggBc27wqDAHg,
	sam.bobroff-8fk3Idey6ehBDgjK7y7TUQ

On 17/09/15 13:03, Peter Senna Tschudin wrote:
> Returning a negative value for a boolean function seem to have the
> undesired effect of returning true. require_paranoia_below() is a
> boolean function, but the variable used to store the return value is an
> integer, receiving -1 or 0. This patch convert rc to bool, replace -1
> by false, and 0 by true.

The issue still seems to be not fixed.

-- 
Cheers,
  Andrew

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-09-02  9:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-17 11:03 [PATCH] tools-powerpc: Return false instead of -1 Peter Senna Tschudin
     [not found] ` <1442487801-22661-1-git-send-email-peter.senna-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2016-09-02  9:54   ` Andrew Shadura

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).