Skip to contentSkip to conversation

strange braces behaviour

cocci Johannes Berg 2 participants 7 messages Jun 4, 2024 – Jun 10, 2024

lore
Share & tools
lore
#0 lore raw

Hi,

So I've been trying a few similar things, but today we came across this one:

Given this function from the current Linux kernel:

void ieee80211_color_change_finish(struct ieee80211_vif *vif, u8 link_id)

{
        struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
        struct ieee80211_link_data *link;

        if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
                return;

        rcu_read_lock();

        link = rcu_dereference(sdata->link[link_id]);
        if (WARN_ON(!link)) {
                rcu_read_unlock();
                return;
        }

        wiphy_work_queue(sdata->local->hw.wiphy,
                         &link->color_change_finalize_work);

        rcu_read_unlock();
}

Applying the following spatch:

@@
identifier func =~ "ieee80211_color_change_finish"; @@
func(...)
{
-...
}

leads to

void ieee80211_color_change_finish(struct ieee80211_vif *vif, u8 link_id) {
}
}

with
spatch version 1.1.1 compiled with OCaml version 5.0.1+dev0-2022-12-15 and also with
spatch version 1.1.1 compiled with OCaml version 4.14.1

Trying this spatch:

@@
identifier func =~ "ieee80211_color_change_finish"; @@
func(...)
-{...}
+{}

works fine with
spatch version 1.1.1 compiled with OCaml version 5.0.1+dev0-2022-12-15

but *fails* with
spatch version 1.1.1 compiled with OCaml version 4.14.1

saying

EXN: Failure("rule starting on line 1: already tagged token:\nC code context\nFile \"./test.c\", line 3, column 0, charpos = 76\n around = '}',\n whole content = }") in ./test.c

which is almost stranger than the left-over closing brace, especially the difference between those two spatch versions I have?

Any thoughts?

(I have a similar situation with removing a whole switch() statement from the code, but I'll leave that for later)

Thanks,
johannes

#1 lore raw
Quoted 49 lines · Johannes Berg

Hi,

So I've been trying a few similar things, but today we came across this one:

Given this function from the current Linux kernel:

void ieee80211_color_change_finish(struct ieee80211_vif *vif, u8 link_id)

{
        struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
        struct ieee80211_link_data *link;

        if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
                return;

        rcu_read_lock();

        link = rcu_dereference(sdata->link[link_id]);
        if (WARN_ON(!link)) {
                rcu_read_unlock();
                return;
        }

        wiphy_work_queue(sdata->local->hw.wiphy,
                         &link->color_change_finalize_work);

        rcu_read_unlock();
}

Applying the following spatch:

@@
identifier func =~ "ieee80211_color_change_finish"; @@
func(...)
{
-...
}

leads to

void ieee80211_color_change_finish(struct ieee80211_vif *vif, u8 link_id) {
}
}

Quoted 4 lines

with
spatch version 1.1.1 compiled with OCaml version 5.0.1+dev0-2022-12-15 and also with
spatch version 1.1.1 compiled with OCaml version 4.14.1

Thanks for the report. I will look into it.

Quoted 24 lines

Trying this spatch:

@@
identifier func =~ "ieee80211_color_change_finish"; @@
func(...)

(quoted diff) +1 -1
-{...}
+{}

works fine with
spatch version 1.1.1 compiled with OCaml version 5.0.1+dev0-2022-12-15

but *fails* with
spatch version 1.1.1 compiled with OCaml version 4.14.1

saying

EXN: Failure("rule starting on line 1: already tagged token:\nC code context\nFile \"./test.c\", line 3, column 0, charpos = 76\n around = '}',\n whole content = }") in ./test.c

Quoted 5 lines

which is almost stranger than the left-over closing brace, especially the difference between those two spatch versions I have?

Any thoughts?

This is indeed much more mysterious...

julia

Quoted 6 lines

(I have a similar situation with removing a whole switch() statement from the code, but I'll leave that for later)

Thanks,
johannes

#2 lore raw
In reply to Johannes Berg
Quoted 41 lines · Johannes Berg

Hi,

So I've been trying a few similar things, but today we came across this one:

Given this function from the current Linux kernel:

void ieee80211_color_change_finish(struct ieee80211_vif *vif, u8 link_id)

{
        struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
        struct ieee80211_link_data *link;

        if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
                return;

        rcu_read_lock();

        link = rcu_dereference(sdata->link[link_id]);
        if (WARN_ON(!link)) {
                rcu_read_unlock();
                return;
        }

        wiphy_work_queue(sdata->local->hw.wiphy,
                         &link->color_change_finalize_work);

        rcu_read_unlock();
}

Applying the following spatch:

@@
identifier func =~ "ieee80211_color_change_finish"; @@
func(...)
{
-...
}

Quoted 6 lines

leads to

void ieee80211_color_change_finish(struct ieee80211_vif *vif, u8 link_id) {
}
}

This problem is fixed. Even though it's just one extra brace, it was a serious problem. Thanks for the report.

I'll have to check on the next one. Thanks.

julia

Quoted 42 lines

with
spatch version 1.1.1 compiled with OCaml version 5.0.1+dev0-2022-12-15 and also with
spatch version 1.1.1 compiled with OCaml version 4.14.1

Trying this spatch:

@@
identifier func =~ "ieee80211_color_change_finish"; @@
func(...)

(quoted diff) +1 -1
-{...}
+{}

works fine with
spatch version 1.1.1 compiled with OCaml version 5.0.1+dev0-2022-12-15

but *fails* with
spatch version 1.1.1 compiled with OCaml version 4.14.1

saying

EXN: Failure("rule starting on line 1: already tagged token:\nC code context\nFile \"./test.c\", line 3, column 0, charpos = 76\n around = '}',\n whole content = }") in ./test.c

which is almost stranger than the left-over closing brace, especially the difference between those two spatch versions I have?

Any thoughts?

(I have a similar situation with removing a whole switch() statement from the code, but I'll leave that for later)

Thanks,
johannes

#3 lore raw
Quoted 7 lines · Julia Lawall
Quoted 5 lines

void ieee80211_color_change_finish(struct ieee80211_vif *vif, u8 link_id) {
}
}

This problem is fixed.

Cool, thanks a lot! :-)

Do you have a pointer to the code or something by any chance? We might be building spatch anyway (need to check, but at least have an easy option to with nix packages), so I might be tempted to pick that fix up before release for our internal automations.

Also, I'd like to check against the similar issue I had with removing a whole switch statement ("-switch(...) {...}")

Quoted 2 lines

Even though it's just one extra brace, it was a
serious problem.

This was also the more simple test case - in other functions I could actually make it leave more closing braces, I think if you had multiple inside the function.

Thanks,
Johannes

#4 lore raw
Quoted 7 lines · Johannes Berg
Quoted 6 lines · Julia Lawall
Quoted 5 lines

void ieee80211_color_change_finish(struct ieee80211_vif *vif, u8 link_id) {
}
}

Quoted 8 lines
Quoted 1 line

This problem is fixed.

Cool, thanks a lot! :-)

Do you have a pointer to the code or something by any chance? We might be building spatch anyway (need to check, but at least have an easy option to with nix packages), so I might be tempted to pick that fix up before release for our internal automations.

commit 7eded3b97dadb1f0f4ed29d6c56856647c04b656

Author: Julia Lawall <[email protected]>
Date:   Sun Jun 9 16:07:57 2024 +0200

should use the non fake end node when after is attached to it; that is
part of the code

Quoted 2 lines

Also, I'd like to check against the similar issue I had with removing a whole switch statement ("-switch(...) {...}")

Maybe it will indeed fix this as well.

Quoted 7 lines
Quoted 3 lines

Even though it's just one extra brace, it was a
serious problem.

This was also the more simple test case - in other functions I could actually make it leave more closing braces, I think if you had multiple inside the function.

Whenever you had { { { { return; } } } }, it would leave all of those closing braces.

julia

#5 lore raw
Quoted 6 lines · Julia Lawall

commit 7eded3b97dadb1f0f4ed29d6c56856647c04b656

Author: Julia Lawall <[email protected]>
Date:   Sun Jun 9 16:07:57 2024 +0200

should use the non fake end node when after is attached to it; that is
part of the code

Thanks.

I built master (commit ac9af0e8b7c76341de1999389ce285b72ebaf81b) now.

Quoted 4 lines
Quoted 2 lines

Also, I'd like to check against the similar issue I had with removing a whole switch statement ("-switch(...) {...}")

Maybe it will indeed fix this as well.

So first of all, it doesn't even parse what I wanted:

@@
@@
sta_link_apply_parameters(...,
- struct link_station_parameters *params)
+ struct station_parameters *params)
{
...
-u32 link_id = params->link_id < 0 ? 0 : params->link_id; +u32 link_id = 0;
...
-bool changes = ...;
...
-switch (mode) { ... }
...
}

but this isn't supported:

minus: parse error:
File "...", line 13, column 17, charpos = 295
around = '...',
whole content = -switch (mode) { ... }

but I guess that's fair.

But trying to do this:

@@
@@
sta_link_apply_parameters(...,
- struct link_station_parameters *params)
+ struct station_parameters *params)
{
...
-u32 link_id = params->link_id < 0 ? 0 : params->link_id; +u32 link_id = 0;
...
-bool changes = ...;
...
-switch (mode) {
-case STA_LINK_MODE_NEW:
-...
-break;
-case STA_LINK_MODE_LINK_MODIFY:
-...
-break;
-case STA_LINK_MODE_STA_MODIFY:
-...
-break;
-}
...
}

There's a "return -EINVAL" inside the _NEW case in the original code:

static int sta_link_apply_parameters(struct ieee80211_local *local,
struct sta_info *sta,
enum sta_link_apply_mode mode, struct link_station_parameters *params)

{
        struct ieee80211_supported_band *sband;
        struct ieee80211_sub_if_data *sdata = sta->sdata;
        u32 link_id = params->link_id < 0 ? 0 : params->link_id;
        struct ieee80211_link_data *link =
                sdata_dereference(sdata->link[link_id], sdata);
        struct link_sta_info *link_sta =
                rcu_dereference_protected(sta->link[link_id],
                                          lockdep_is_held(&local->hw.wiphy->mtx));
        bool changes = params->link_mac ||
                       params->txpwr_set ||
                       params->supported_rates_len ||
                       params->ht_capa ||
                       params->vht_capa ||
                       params->he_capa ||
                       params->eht_capa ||
                       params->opmode_notif_used;

        switch (mode) {
        case STA_LINK_MODE_NEW:
                if (!params->link_mac)
                        return -EINVAL;
                break;
        case STA_LINK_MODE_LINK_MODIFY:
                break;
        case STA_LINK_MODE_STA_MODIFY:
                if (params->link_id >= 0)
                        break;
                if (!changes)
                        return 0;
                break;
        }

...

which spatch leaves around:

@@ -1814,113 +1814,93 @@ enum sta_link_apply_mode {
static int sta_link_apply_parameters(struct ieee80211_local *local,
struct sta_info *sta,
enum sta_link_apply_mode mode,
- struct link_station_parameters *params)
+ struct station_parameters *params)
{
struct ieee80211_supported_band *sband;
struct ieee80211_sub_if_data *sdata = sta->sdata;
- u32 link_id = params->link_id < 0 ? 0 : params->link_id;
+ u32 link_id = 0;
struct ieee80211_link_data *link =
sdata_dereference(sdata->link[link_id], sdata);
struct link_sta_info *link_sta =
rcu_dereference_protected(sta->link[link_id],
lockdep_is_held(&local->hw.wiphy->mtx));
- bool changes = params->link_mac ||

-		       params->txpwr_set ||
-		       params->supported_rates_len ||
-		       params->ht_capa ||
-		       params->vht_capa ||
-		       params->he_capa ||
-		       params->eht_capa ||
-		       params->opmode_notif_used;

-
- switch (mode) {
- case STA_LINK_MODE_NEW:
- if (!params->link_mac)
- return -EINVAL;
- break;
- case STA_LINK_MODE_LINK_MODIFY:
- break;
- case STA_LINK_MODE_STA_MODIFY:
- if (params->link_id >= 0)
- break;
- if (!changes)
- return 0;
- break;
- }
+ return -EINVAL;
+ return 0;
+}

and it also has the closing brace here, so it somehow leaves a closing brace too much also in this case, even with the fix.

So perhaps not the same issue?

(I worked around it for now by leave the switch/case/break instead of removing, so it's kind of just a dead switch after the change, doesn't do anything but structurally still exists)

Thanks,
johannes

#6 lore raw
Quoted 35 lines · Johannes Berg
Quoted 6 lines · Julia Lawall

commit 7eded3b97dadb1f0f4ed29d6c56856647c04b656

Author: Julia Lawall <[email protected]>
Date:   Sun Jun 9 16:07:57 2024 +0200

should use the non fake end node when after is attached to it; that is
part of the code

Thanks.

I built master (commit ac9af0e8b7c76341de1999389ce285b72ebaf81b) now.

Quoted 4 lines
Quoted 2 lines

Also, I'd like to check against the similar issue I had with removing a whole switch statement ("-switch(...) {...}")

Maybe it will indeed fix this as well.

So first of all, it doesn't even parse what I wanted:

@@
@@
sta_link_apply_parameters(...,

(quoted diff) +1 -1
-                          struct link_station_parameters *params)
+                          struct station_parameters *params)

{
...

(quoted diff) +1 -3
-u32 link_id = params->link_id < 0 ? 0 : params->link_id;
+u32 link_id = 0;
...
-bool changes = ...;
...
-switch (mode) { ... }
...

}

Quoted 8 lines

but this isn't supported:

minus: parse error:
File "...", line 13, column 17, charpos = 295
around = '...',
whole content = -switch (mode) { ... }

but I guess that's fair.

Thanks for the report.

It might work to write

{
switch ... // all the detail you put below
&
- S
)

where S ia a statement metavariable.

I haven't tried it though.

julia

Quoted 128 lines

But trying to do this:

@@
@@
sta_link_apply_parameters(...,

(quoted diff) +1 -1
-                          struct link_station_parameters *params)
+                          struct station_parameters *params)

{
...

(quoted diff) +1 -13
-u32 link_id = params->link_id < 0 ? 0 : params->link_id;
+u32 link_id = 0;
...
-bool changes = ...;
...
-switch (mode) {
-case STA_LINK_MODE_NEW:
-...
-break;
-case STA_LINK_MODE_LINK_MODIFY:
-...
-break;
-case STA_LINK_MODE_STA_MODIFY:
-...
-break;
-}
...

}

There's a "return -EINVAL" inside the _NEW case in the original code:

static int sta_link_apply_parameters(struct ieee80211_local *local,
struct sta_info *sta,
enum sta_link_apply_mode mode, struct link_station_parameters *params)

{
        struct ieee80211_supported_band *sband;
        struct ieee80211_sub_if_data *sdata = sta->sdata;
        u32 link_id = params->link_id < 0 ? 0 : params->link_id;
        struct ieee80211_link_data *link =
                sdata_dereference(sdata->link[link_id], sdata);
        struct link_sta_info *link_sta =
                rcu_dereference_protected(sta->link[link_id],
                                          lockdep_is_held(&local->hw.wiphy->mtx));
        bool changes = params->link_mac ||
                       params->txpwr_set ||
                       params->supported_rates_len ||
                       params->ht_capa ||
                       params->vht_capa ||
                       params->he_capa ||
                       params->eht_capa ||
                       params->opmode_notif_used;

        switch (mode) {
        case STA_LINK_MODE_NEW:
                if (!params->link_mac)
                        return -EINVAL;
                break;
        case STA_LINK_MODE_LINK_MODIFY:
                break;
        case STA_LINK_MODE_STA_MODIFY:
                if (params->link_id >= 0)
                        break;
                if (!changes)
                        return 0;
                break;
        }

...

which spatch leaves around:

(quoted diff) +5 -25
@@ -1814,113 +1814,93 @@ enum sta_link_apply_mode {
 static int sta_link_apply_parameters(struct ieee80211_local *local,
 				     struct sta_info *sta,
 				     enum sta_link_apply_mode mode,
-				     struct link_station_parameters *params)
+				     struct station_parameters *params)
 {
 	struct ieee80211_supported_band *sband;
 	struct ieee80211_sub_if_data *sdata = sta->sdata;
-	u32 link_id = params->link_id < 0 ? 0 : params->link_id;
+	u32 link_id = 0;
 	struct ieee80211_link_data *link =
 		sdata_dereference(sdata->link[link_id], sdata);
 	struct link_sta_info *link_sta =
 		rcu_dereference_protected(sta->link[link_id],
 					  lockdep_is_held(&local->hw.wiphy->mtx));
-	bool changes = params->link_mac ||
-		       params->txpwr_set ||
-		       params->supported_rates_len ||
-		       params->ht_capa ||
-		       params->vht_capa ||
-		       params->he_capa ||
-		       params->eht_capa ||
-		       params->opmode_notif_used;
-
-	switch (mode) {
-	case STA_LINK_MODE_NEW:
-		if (!params->link_mac)
-			return -EINVAL;
-		break;
-	case STA_LINK_MODE_LINK_MODIFY:
-		break;
-	case STA_LINK_MODE_STA_MODIFY:
-		if (params->link_id >= 0)
-			break;
-		if (!changes)
-			return 0;
-		break;
-	}
+	return -EINVAL;
+	return 0;
+}
 

and it also has the closing brace here, so it somehow leaves a closing brace too much also in this case, even with the fix.

So perhaps not the same issue?

(I worked around it for now by leave the switch/case/break instead of removing, so it's kind of just a dead switch after the change, doesn't do anything but structurally still exists)

Thanks,
johannes