Skip to contentSkip to conversation

tools-powerpc: Return false instead of -1

PATCH In review Peter Senna Tschudin 2 participants 2 messages Sep 17, 2015 – Sep 2, 2016

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools-powerpc: Return false instead of -1

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>

---
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(-)
tools/testing/selftests/powerpc/pmu/lib.c +3 -3
index a07104c..2f17110 100644
@@ -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:
Signature · 1 line
2.1.0
#1 lore raw
Quoted 5 lines · 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.

The issue still seems to be not fixed.

Signature · 2 lines
Cheers,
  Andrew