Skip to content

Commit edc22a7

Browse files
JustinStittmartinkpetersen
authored andcommitted
scsi: fcoe: Use sysfs_match_string() over fcoe_parse_mode()
Instead of copying @buf into a new buffer and carefully managing its newline/null-terminating status, we can just use sysfs_match_string() as it uses sysfs_streq() internally which handles newline/null-term: | /** | * sysfs_streq - return true if strings are equal, modulo trailing newline | * @s1: one string | * @S2: another string | * | * This routine returns true iff two strings are equal, treating both | * NUL and newline-then-NUL as equivalent string terminations. It's | * geared for use with sysfs input strings, which generally terminate | * with newlines but are compared against values without newlines. | */ | bool sysfs_streq(const char *s1, const char *s2) | ... Then entirely drop the now unused fcoe_parse_mode(), being careful to change if condition from checking for FIP_CONN_TYPE_UNKNOWN to < 0 as sysfs_match_string() can return -EINVAL. Also check explicitly if ctlr->mode is equal to FIP_CONN_TYPE_UNKNOWN -- this is probably preferred to "<=" as the behavior is more obvious while maintaining functionality. To get the compiler not to complain, make fip_conn_type_names const char * const. Perhaps, this should also be done for fcf_state_names. This also removes an instance of strncpy() which helps [1]. Link: KSPP#90 [1] Cc: <[email protected]> Signed-off-by: Justin Stitt <[email protected]> Link: https://lore.kernel.org/r/20231212-strncpy-drivers-scsi-fcoe-fcoe_sysfs-c-v2-1-1f2d6b2fc409@google.com Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Hannes Reinecke <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 33c3e71 commit edc22a7

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed

drivers/scsi/fcoe/fcoe_sysfs.c

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/kernel.h>
1111
#include <linux/etherdevice.h>
1212
#include <linux/ctype.h>
13+
#include <linux/string.h>
1314

1415
#include <scsi/fcoe_sysfs.h>
1516
#include <scsi/libfcoe.h>
@@ -214,25 +215,13 @@ static const char *get_fcoe_##title##_name(enum table_type table_key) \
214215
return table[table_key]; \
215216
}
216217

217-
static char *fip_conn_type_names[] = {
218+
static const char * const fip_conn_type_names[] = {
218219
[ FIP_CONN_TYPE_UNKNOWN ] = "Unknown",
219220
[ FIP_CONN_TYPE_FABRIC ] = "Fabric",
220221
[ FIP_CONN_TYPE_VN2VN ] = "VN2VN",
221222
};
222223
fcoe_enum_name_search(ctlr_mode, fip_conn_type, fip_conn_type_names)
223224

224-
static enum fip_conn_type fcoe_parse_mode(const char *buf)
225-
{
226-
int i;
227-
228-
for (i = 0; i < ARRAY_SIZE(fip_conn_type_names); i++) {
229-
if (strcasecmp(buf, fip_conn_type_names[i]) == 0)
230-
return i;
231-
}
232-
233-
return FIP_CONN_TYPE_UNKNOWN;
234-
}
235-
236225
static char *fcf_state_names[] = {
237226
[ FCOE_FCF_STATE_UNKNOWN ] = "Unknown",
238227
[ FCOE_FCF_STATE_DISCONNECTED ] = "Disconnected",
@@ -274,17 +263,10 @@ static ssize_t store_ctlr_mode(struct device *dev,
274263
const char *buf, size_t count)
275264
{
276265
struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
277-
char mode[FCOE_MAX_MODENAME_LEN + 1];
278266

279267
if (count > FCOE_MAX_MODENAME_LEN)
280268
return -EINVAL;
281269

282-
strncpy(mode, buf, count);
283-
284-
if (mode[count - 1] == '\n')
285-
mode[count - 1] = '\0';
286-
else
287-
mode[count] = '\0';
288270

289271
switch (ctlr->enabled) {
290272
case FCOE_CTLR_ENABLED:
@@ -297,8 +279,8 @@ static ssize_t store_ctlr_mode(struct device *dev,
297279
return -ENOTSUPP;
298280
}
299281

300-
ctlr->mode = fcoe_parse_mode(mode);
301-
if (ctlr->mode == FIP_CONN_TYPE_UNKNOWN) {
282+
ctlr->mode = sysfs_match_string(fip_conn_type_names, buf);
283+
if (ctlr->mode < 0 || ctlr->mode == FIP_CONN_TYPE_UNKNOWN) {
302284
LIBFCOE_SYSFS_DBG(ctlr, "Unknown mode %s provided.\n",
303285
buf);
304286
return -EINVAL;

0 commit comments

Comments
 (0)