Skip to content

Commit 7037c96

Browse files
ZhaoLong Wangrichardweinberger
authored andcommitted
ubifs: correct UBIFS_DFS_DIR_LEN macro definition and improve code clarity
The UBIFS_DFS_DIR_LEN macro, which defines the maximum length of the UBIFS debugfs directory name, has an incorrect formula and misleading comments. The current formula is (3 + 1 + 2*2 + 1), which assumes that both UBI device number and volume ID are limited to 2 characters. However, UBI device number ranges from 0 to 31 (2 characters), and volume ID ranges from 0 to 127 (up to 3 characters). Although the current code works due to the cancellation of mathematical errors (9 + 1 = 10, which matches the correct UBIFS_DFS_DIR_LEN value), it can lead to confusion and potential issues in the future. This patch aims to improve the code clarity and maintainability by making the following changes: 1. Corrects the UBIFS_DFS_DIR_LEN macro definition to (3 + 1 + 2 + 3 + 1), accommodating the maximum lengths of both UBI device number and volume ID, plus the separators and null terminator. 2. Updates the snprintf calls to use UBIFS_DFS_DIR_LEN instead of UBIFS_DFS_DIR_LEN + 1, removing the unnecessary +1. 3. Modifies the error checks to compare against UBIFS_DFS_DIR_LEN using >= instead of >, aligning with the corrected macro definition. 4. Removes the redundant +1 in the dfs_dir_name array definitions in ubi.h and debug.h. While these changes do not affect the runtime behavior, they make the code more readable, maintainable, and less prone to future errors. v2->v3: - Removes the duplicated UBIFS_DFS_DIR_LEN and UBIFS_DFS_DIR_NAME macro definitions in ubifs.h, as they are already defined in debug.h. Signed-off-by: ZhaoLong Wang <wangzhaolong1@huawei.com> Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com> Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent 72f3d3d commit 7037c96

File tree

6 files changed

+12
-18
lines changed

6 files changed

+12
-18
lines changed

drivers/mtd/ubi/debug.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,9 @@ int ubi_debugfs_init_dev(struct ubi_device *ubi)
598598
if (!IS_ENABLED(CONFIG_DEBUG_FS))
599599
return 0;
600600

601-
n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
601+
n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN, UBI_DFS_DIR_NAME,
602602
ubi->ubi_num);
603-
if (n > UBI_DFS_DIR_LEN) {
603+
if (n >= UBI_DFS_DIR_LEN) {
604604
/* The array size is too small */
605605
return -EINVAL;
606606
}

drivers/mtd/ubi/ubi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ struct ubi_debug_info {
420420
unsigned int power_cut_min;
421421
unsigned int power_cut_max;
422422
unsigned int emulate_failures;
423-
char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
423+
char dfs_dir_name[UBI_DFS_DIR_LEN];
424424
struct dentry *dfs_dir;
425425
struct dentry *dfs_chk_gen;
426426
struct dentry *dfs_chk_io;

fs/ubifs/debug.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,9 +2827,9 @@ void dbg_debugfs_init_fs(struct ubifs_info *c)
28272827
const char *fname;
28282828
struct ubifs_debug_info *d = c->dbg;
28292829

2830-
n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2830+
n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN, UBIFS_DFS_DIR_NAME,
28312831
c->vi.ubi_num, c->vi.vol_id);
2832-
if (n > UBIFS_DFS_DIR_LEN) {
2832+
if (n >= UBIFS_DFS_DIR_LEN) {
28332833
/* The array size is too small */
28342834
return;
28352835
}

fs/ubifs/debug.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ typedef int (*dbg_znode_callback)(struct ubifs_info *c,
1919

2020
/*
2121
* The UBIFS debugfs directory name pattern and maximum name length (3 for "ubi"
22-
* + 1 for "_" and plus 2x2 for 2 UBI numbers and 1 for the trailing zero byte.
22+
* + 1 for "_" and 2 for UBI device numbers and 3 for volume number and 1 for
23+
* the trailing zero byte.
2324
*/
2425
#define UBIFS_DFS_DIR_NAME "ubi%d_%d"
25-
#define UBIFS_DFS_DIR_LEN (3 + 1 + 2*2 + 1)
26+
#define UBIFS_DFS_DIR_LEN (3 + 1 + 2 + 3 + 1)
2627

2728
/**
2829
* ubifs_debug_info - per-FS debugging information.
@@ -103,7 +104,7 @@ struct ubifs_debug_info {
103104
unsigned int chk_fs:1;
104105
unsigned int tst_rcvry:1;
105106

106-
char dfs_dir_name[UBIFS_DFS_DIR_LEN + 1];
107+
char dfs_dir_name[UBIFS_DFS_DIR_LEN];
107108
struct dentry *dfs_dir;
108109
struct dentry *dfs_dump_lprops;
109110
struct dentry *dfs_dump_budg;

fs/ubifs/sysfs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,17 @@ static struct kset ubifs_kset = {
9191
int ubifs_sysfs_register(struct ubifs_info *c)
9292
{
9393
int ret, n;
94-
char dfs_dir_name[UBIFS_DFS_DIR_LEN+1];
94+
char dfs_dir_name[UBIFS_DFS_DIR_LEN];
9595

9696
c->stats = kzalloc(sizeof(struct ubifs_stats_info), GFP_KERNEL);
9797
if (!c->stats) {
9898
ret = -ENOMEM;
9999
goto out_last;
100100
}
101-
n = snprintf(dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
101+
n = snprintf(dfs_dir_name, UBIFS_DFS_DIR_LEN, UBIFS_DFS_DIR_NAME,
102102
c->vi.ubi_num, c->vi.vol_id);
103103

104-
if (n > UBIFS_DFS_DIR_LEN) {
104+
if (n >= UBIFS_DFS_DIR_LEN) {
105105
/* The array size is too small */
106106
ret = -EINVAL;
107107
goto out_free;

fs/ubifs/ubifs.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,6 @@
157157
#define UBIFS_HMAC_ARR_SZ 0
158158
#endif
159159

160-
/*
161-
* The UBIFS sysfs directory name pattern and maximum name length (3 for "ubi"
162-
* + 1 for "_" and plus 2x2 for 2 UBI numbers and 1 for the trailing zero byte.
163-
*/
164-
#define UBIFS_DFS_DIR_NAME "ubi%d_%d"
165-
#define UBIFS_DFS_DIR_LEN (3 + 1 + 2*2 + 1)
166-
167160
/*
168161
* Lockdep classes for UBIFS inode @ui_mutex.
169162
*/

0 commit comments

Comments
 (0)