diff mbox series

[1/2] wifi: cfg80211: avoid extra calls to strlen() in ieee80211_bss()

Message ID 20230830043346.28303-1-dmantipov@yandex.ru
State Superseded
Headers show
Series [1/2] wifi: cfg80211: avoid extra calls to strlen() in ieee80211_bss() | expand

Commit Message

Dmitry Antipov Aug. 30, 2023, 4:33 a.m. UTC
Since 'sprintf()' returns the number of characters emitted, an
extra calls to 'strlen()' in 'ieee80211_bss()' may be dropped.

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 net/wireless/scan.c | 42 ++++++++++++++++++++----------------------
 1 file changed, 20 insertions(+), 22 deletions(-)

Comments

Johannes Berg Sept. 11, 2023, 10:11 a.m. UTC | #1
On Wed, 2023-08-30 at 07:33 +0300, Dmitry Antipov wrote:
> Since 'sprintf()' returns the number of characters emitted, an
> extra calls to 'strlen()' in 'ieee80211_bss()' may be dropped.

This should probably mention wext/"wireless extensions" somewhere - I
got confused at first why we even had strlen() :-)

> Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
> ---
>  net/wireless/scan.c | 42 ++++++++++++++++++++----------------------
>  1 file changed, 20 insertions(+), 22 deletions(-)
> 
> diff --git a/net/wireless/scan.c b/net/wireless/scan.c
> index 0cf1ce7b6934..4f4990ca7ba7 100644
> --- a/net/wireless/scan.c
> +++ b/net/wireless/scan.c
> @@ -3422,59 +3422,58 @@ ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
>  			cfg = (u8 *)ie + 2;
>  			memset(&iwe, 0, sizeof(iwe));
>  			iwe.cmd = IWEVCUSTOM;
> -			sprintf(buf, "Mesh Network Path Selection Protocol ID: "
> -				"0x%02X", cfg[0]);
> -			iwe.u.data.length = strlen(buf);
> +			iwe.u.data.length = sprintf(buf, "Mesh Network Path "
> +						    "Selection Protocol ID: "
> +						    "0x%02X", cfg[0]);

I don't think we should make this worse and break the strings in _more_
places ... it's OK to go above 80 columns for strings.

So probably better written as

sprintf(buf,
        "....",
        ...);

here and in all the other places too.

johannes
diff mbox series

Patch

diff --git a/net/wireless/scan.c b/net/wireless/scan.c
index 0cf1ce7b6934..4f4990ca7ba7 100644
--- a/net/wireless/scan.c
+++ b/net/wireless/scan.c
@@ -3422,59 +3422,58 @@  ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
 			cfg = (u8 *)ie + 2;
 			memset(&iwe, 0, sizeof(iwe));
 			iwe.cmd = IWEVCUSTOM;
-			sprintf(buf, "Mesh Network Path Selection Protocol ID: "
-				"0x%02X", cfg[0]);
-			iwe.u.data.length = strlen(buf);
+			iwe.u.data.length = sprintf(buf, "Mesh Network Path "
+						    "Selection Protocol ID: "
+						    "0x%02X", cfg[0]);
 			current_ev = iwe_stream_add_point_check(info,
 								current_ev,
 								end_buf,
 								&iwe, buf);
 			if (IS_ERR(current_ev))
 				goto unlock;
-			sprintf(buf, "Path Selection Metric ID: 0x%02X",
-				cfg[1]);
-			iwe.u.data.length = strlen(buf);
+			iwe.u.data.length = sprintf(buf, "Path Selection "
+						    "Metric ID: 0x%02X",
+						    cfg[1]);
 			current_ev = iwe_stream_add_point_check(info,
 								current_ev,
 								end_buf,
 								&iwe, buf);
 			if (IS_ERR(current_ev))
 				goto unlock;
-			sprintf(buf, "Congestion Control Mode ID: 0x%02X",
-				cfg[2]);
-			iwe.u.data.length = strlen(buf);
+			iwe.u.data.length = sprintf(buf, "Congestion Control "
+						    "Mode ID: 0x%02X", cfg[2]);
 			current_ev = iwe_stream_add_point_check(info,
 								current_ev,
 								end_buf,
 								&iwe, buf);
 			if (IS_ERR(current_ev))
 				goto unlock;
-			sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
-			iwe.u.data.length = strlen(buf);
+			iwe.u.data.length = sprintf(buf, "Synchronization ID: "
+						    "0x%02X", cfg[3]);
 			current_ev = iwe_stream_add_point_check(info,
 								current_ev,
 								end_buf,
 								&iwe, buf);
 			if (IS_ERR(current_ev))
 				goto unlock;
-			sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
-			iwe.u.data.length = strlen(buf);
+			iwe.u.data.length = sprintf(buf, "Authentication ID: "
+						    "0x%02X", cfg[4]);
 			current_ev = iwe_stream_add_point_check(info,
 								current_ev,
 								end_buf,
 								&iwe, buf);
 			if (IS_ERR(current_ev))
 				goto unlock;
-			sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
-			iwe.u.data.length = strlen(buf);
+			iwe.u.data.length = sprintf(buf, "Formation Info: "
+						    "0x%02X", cfg[5]);
 			current_ev = iwe_stream_add_point_check(info,
 								current_ev,
 								end_buf,
 								&iwe, buf);
 			if (IS_ERR(current_ev))
 				goto unlock;
-			sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
-			iwe.u.data.length = strlen(buf);
+			iwe.u.data.length = sprintf(buf, "Capabilities: "
+						    "0x%02X", cfg[6]);
 			current_ev = iwe_stream_add_point_check(info,
 								current_ev,
 								end_buf,
@@ -3530,17 +3529,16 @@  ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
 
 	memset(&iwe, 0, sizeof(iwe));
 	iwe.cmd = IWEVCUSTOM;
-	sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
-	iwe.u.data.length = strlen(buf);
+	iwe.u.data.length = sprintf(buf, "tsf=%016llx",
+				    (unsigned long long)(ies->tsf));
 	current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
 						&iwe, buf);
 	if (IS_ERR(current_ev))
 		goto unlock;
 	memset(&iwe, 0, sizeof(iwe));
 	iwe.cmd = IWEVCUSTOM;
-	sprintf(buf, " Last beacon: %ums ago",
-		elapsed_jiffies_msecs(bss->ts));
-	iwe.u.data.length = strlen(buf);
+	iwe.u.data.length = sprintf(buf, " Last beacon: %ums ago",
+				    elapsed_jiffies_msecs(bss->ts));
 	current_ev = iwe_stream_add_point_check(info, current_ev,
 						end_buf, &iwe, buf);
 	if (IS_ERR(current_ev))