Skip to contentSkip to conversation

Missing closing bracket

Cocci Jeff Mahoney 2 participants 3 messages Jun 14, 2016 – Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hi all -

I'm working on a project to transform the btrfs code into something more approachable by cleaning up some unnecessary code. The specific issue I'm running into right now is that fs/btrfs/transaction.c gets stuck. After playing around with some matching rules last night, I believe I found a bug where Coccinelle is misinterpreting a closing bracket. This is code originally from btrfs_commit_transaction_async, but I've pared it down to the minimum to reproduce the problem.

I had originally encountered this with v1.0.4 but have reproduced with v1.0.5+ (commit 8906ed7e4dda8b). I'd appreciate any help you can give in sorting this out.

Here's the test code:
------>8------
int func(struct btrfs_trans_handle *trans, struct btrfs_root *root)

{
        if (!root) {
                return err;
        }

        __sb_writers_release(root->fs_info->sb, SB_FREEZE_FS);

        return 0;
}

------8<------

The rules are:

------>8------
@root_fs_info@
identifier func;
identifier R;
parameter list[npspre] pspre;
parameter list[npspost] pspost;
@@
func(pspre, struct btrfs_root *R, pspost)
{
...
R->fs_info
...
}

@fs_info_helper@
identifier root_fs_info.func;
identifier root_fs_info.R;
idexpression struct btrfs_fs_info *F;
@@
func(...)
{
* ... when != F = R->fs_info;
}
------<8------

And the output:

------>8------
jeffm at starscream:/src/git/linux/linux-upstream> spatch --sp-file btrfs-cleanup3.cocci tmp.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: tmp.c

tmp/cocci-output-8357-3ffc6d-tmp.c renamed from tmp.c +0 -4
diff =
@@ -1,9 +1,5 @@
 int func(struct btrfs_trans_handle *trans, struct btrfs_root *root)
 {
-	if (!root) {
-		return err;
 	}
 
-	__sb_writers_release(root->fs_info->sb, SB_FREEZE_FS);
-	return 0;
 }

------8<------

... and we end up with:

------>8------
int func(struct btrfs_trans_handle *trans, struct btrfs_root *root)

{
        }

}
------<8------

If I turn that match rule into:
- ... when != F = R->fs_info;

I get:

tmp/cocci-output-8603-f3e3b9-tmp.c renamed from tmp.c +1 -7
@@ -6,11 +6,5 @@ int btrfs_commit_transaction_async(struc
 				   struct btrfs_root *root,
 				   int wait_for_unblock)
 {
-	if (!root) {
-		return err;
-	}
-
-	__sb_writers_release(root->fs_info->sb, SB_FREEZE_FS);
-
-	return 0;
+}
 }

-Jeff

Signature · 10 lines
Jeff Mahoney
SUSE Labs

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 881 bytes
Desc: OpenPGP digital signature
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20160614/7190b375/attachment.asc>
#1 lore raw
Quoted 96 lines · Jeff Mahoney

Hi all -

I'm working on a project to transform the btrfs code into something more approachable by cleaning up some unnecessary code. The specific issue I'm running into right now is that fs/btrfs/transaction.c gets stuck. After playing around with some matching rules last night, I believe I found a bug where Coccinelle is misinterpreting a closing bracket. This is code originally from btrfs_commit_transaction_async, but I've pared it down to the minimum to reproduce the problem.

I had originally encountered this with v1.0.4 but have reproduced with v1.0.5+ (commit 8906ed7e4dda8b). I'd appreciate any help you can give in sorting this out.

Here's the test code:
------>8------
int func(struct btrfs_trans_handle *trans, struct btrfs_root *root)

{
        if (!root) {
                return err;
        }

        __sb_writers_release(root->fs_info->sb, SB_FREEZE_FS);

        return 0;
}

------8<------

The rules are:

------>8------
@root_fs_info@
identifier func;
identifier R;
parameter list[npspre] pspre;
parameter list[npspost] pspost;
@@
func(pspre, struct btrfs_root *R, pspost)
{
...
R->fs_info
...
}

@fs_info_helper@
identifier root_fs_info.func;
identifier root_fs_info.R;
idexpression struct btrfs_fs_info *F;
@@
func(...)
{
* ... when != F = R->fs_info;
}
------<8------

And the output:

------>8------
jeffm at starscream:/src/git/linux/linux-upstream> spatch --sp-file btrfs-cleanup3.cocci tmp.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: tmp.c
diff =

tmp/cocci-output-8357-3ffc6d-tmp.c renamed from tmp.c +0 -5
@@ -1,9 +1,5 @@
 int func(struct btrfs_trans_handle *trans, struct btrfs_root *root)
 {
-	if (!root) {
-		return err;
 	}
 
-	__sb_writers_release(root->fs_info->sb, SB_FREEZE_FS);
-	return 0;
 }
------8<------

... and we end up with:

------>8------
int func(struct btrfs_trans_handle *trans, struct btrfs_root *root)

{
        }

}
------<8------

If I turn that match rule into:
- ... when != F = R->fs_info;

I get:

tmp/cocci-output-8603-f3e3b9-tmp.c renamed from tmp.c +0 -0
@@ -6,11 +6,5 @@ int btrfs_commit_transaction_async(struc
 				   struct btrfs_root *root,
 				   int wait_for_unblock)

tmp/cocci-output-8603-f3e3b9-tmp.c int btrfs_commit_transaction_async(struc · line 10

Quoted 10 lines
tmp/cocci-output-8603-f3e3b9-tmp.c renamed from tmp.c +1 -7
 {
-	if (!root) {
-		return err;
-	}
-
-	__sb_writers_release(root->fs_info->sb, SB_FREEZE_FS);
-
-	return 0;
+}
 }

Thanks for the report. It is probably related to the return err somehow. I will look into it. A temporary solution might be obtained by doing the following in the functions of interest:

- return e;
+ Return(e);

But that might not be acceptable for your real application.

By the way, you shouldn't actually apply the patch you get from a semantic patch that has * in the leftmost column. The resulting semantic patch is only intended to be looked at, and to be used with emacs's diff mode (or something similar). But you get essentially the same result with - in the leftmost column, so that isn't the problem here.

julia

#2 lore raw
Quoted 113 lines · Julia Lawall
Quoted 96 lines · Jeff Mahoney

Hi all -

I'm working on a project to transform the btrfs code into something more approachable by cleaning up some unnecessary code. The specific issue I'm running into right now is that fs/btrfs/transaction.c gets stuck. After playing around with some matching rules last night, I believe I found a bug where Coccinelle is misinterpreting a closing bracket. This is code originally from btrfs_commit_transaction_async, but I've pared it down to the minimum to reproduce the problem.

I had originally encountered this with v1.0.4 but have reproduced with v1.0.5+ (commit 8906ed7e4dda8b). I'd appreciate any help you can give in sorting this out.

Here's the test code:
------>8------
int func(struct btrfs_trans_handle *trans, struct btrfs_root *root)

{
        if (!root) {
                return err;
        }

        __sb_writers_release(root->fs_info->sb, SB_FREEZE_FS);

        return 0;
}

------8<------

The rules are:

------>8------
@root_fs_info@
identifier func;
identifier R;
parameter list[npspre] pspre;
parameter list[npspost] pspost;
@@
func(pspre, struct btrfs_root *R, pspost)
{
...
R->fs_info
...
}

@fs_info_helper@
identifier root_fs_info.func;
identifier root_fs_info.R;
idexpression struct btrfs_fs_info *F;
@@
func(...)
{
* ... when != F = R->fs_info;
}
------<8------

And the output:

------>8------
jeffm at starscream:/src/git/linux/linux-upstream> spatch --sp-file btrfs-cleanup3.cocci tmp.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: tmp.c
diff =

tmp/cocci-output-8357-3ffc6d-tmp.c renamed from tmp.c +0 -5
@@ -1,9 +1,5 @@
 int func(struct btrfs_trans_handle *trans, struct btrfs_root *root)
 {
-	if (!root) {
-		return err;
 	}
 
-	__sb_writers_release(root->fs_info->sb, SB_FREEZE_FS);
-	return 0;
 }
------8<------

... and we end up with:

------>8------
int func(struct btrfs_trans_handle *trans, struct btrfs_root *root)

{
        }

}
------<8------

If I turn that match rule into:
- ... when != F = R->fs_info;

I get:

tmp/cocci-output-8603-f3e3b9-tmp.c renamed from tmp.c +0 -0
@@ -6,11 +6,5 @@ int btrfs_commit_transaction_async(struc
 				   struct btrfs_root *root,
 				   int wait_for_unblock)

tmp/cocci-output-8603-f3e3b9-tmp.c int btrfs_commit_transaction_async(struc · line 10

Quoted 10 lines
tmp/cocci-output-8603-f3e3b9-tmp.c renamed from tmp.c +1 -7
 {
-	if (!root) {
-		return err;
-	}
-
-	__sb_writers_release(root->fs_info->sb, SB_FREEZE_FS);
-
-	return 0;
+}
 }

Thanks for the report. It is probably related to the return err somehow. I will look into it. A temporary solution might be obtained by doing the following in the functions of interest:

Quoted 4 lines

- return e;
+ Return(e);

But that might not be acceptable for your real application.

If it's an obvious transformation, I can reverse it later easily enough.
That worked, it handles the brackets correctly now.

Quoted 5 lines

By the way, you shouldn't actually apply the patch you get from a semantic patch that has * in the leftmost column. The resulting semantic patch is only intended to be looked at, and to be used with emacs's diff mode (or something similar). But you get essentially the same result with - in the leftmost column, so that isn't the problem here.

Yep. I was using the match rule as a way to track down what it was doing without requiring it to modify the code. The real ruleset uses -/+ rules.

-Jeff

Signature · 10 lines
Jeff Mahoney
SUSE Labs

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 881 bytes
Desc: OpenPGP digital signature
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20160615/8016d1c0/attachment.asc>