diff mbox series

[v1] tmpfs: Unsigned expression compared with zero

Message ID 20241120105150.24008-1-guanjing@cmss.chinamobile.com (mailing list archive)
State New
Headers show
Series [v1] tmpfs: Unsigned expression compared with zero | expand

Commit Message

guanjing Nov. 20, 2024, 10:51 a.m. UTC
The return value from the call to utf8_parse_version() is not
of the unsigned type.

However, the return value is being assigned to an unsigned int
variable 'version'. This will result in the inability to handle
errors that occur when parsing a UTF-8 version number from
a string.

Additionally, this patch can help eliminate the following
Coccicheck warning:

mm/shmem.c:4378:6-13: WARNING: Unsigned expression compared with zero: version < 0

Fixes: 58e55efd6c72 ("tmpfs: Add casefold lookup support")
Signed-off-by: guanjing <guanjing@cmss.chinamobile.com>
---
 fs/unicode/utf8-core.c  | 9 +++++----
 include/linux/unicode.h | 2 +-
 mm/shmem.c              | 5 +++--
 3 files changed, 9 insertions(+), 7 deletions(-)

Comments

André Almeida Nov. 20, 2024, 11:10 a.m. UTC | #1
Hi guanjing,

Em 20/11/2024 07:51, guanjing escreveu:
> The return value from the call to utf8_parse_version() is not
> of the unsigned type.
> 
> However, the return value is being assigned to an unsigned int
> variable 'version'. This will result in the inability to handle
> errors that occur when parsing a UTF-8 version number from
> a string.
> 
> Additionally, this patch can help eliminate the following
> Coccicheck warning:
> 
> mm/shmem.c:4378:6-13: WARNING: Unsigned expression compared with zero: version < 0
> 
> Fixes: 58e55efd6c72 ("tmpfs: Add casefold lookup support")
> Signed-off-by: guanjing <guanjing@cmss.chinamobile.com>

Another fix was already sent: 
https://lore.kernel.org/lkml/20241111-unsignedcompare1601569-v1-1-c4a9c3c75a52@gmail.com/
kernel test robot Nov. 22, 2024, 3:31 a.m. UTC | #2
Hi guanjing,

kernel test robot noticed the following build warnings:

[auto build test WARNING on brauner-vfs/vfs.all]
[also build test WARNING on linus/master next-20241121]
[cannot apply to v6.12]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/guanjing/tmpfs-Unsigned-expression-compared-with-zero/20241121-152806
base:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all
patch link:    https://lore.kernel.org/r/20241120105150.24008-1-guanjing%40cmss.chinamobile.com
patch subject: [PATCH v1] tmpfs: Unsigned expression compared with zero
config: arc-randconfig-001-20241122 (https://download.01.org/0day-ci/archive/20241122/202411221128.qSNFAkty-lkp@intel.com/config)
compiler: arc-elf-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241122/202411221128.qSNFAkty-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202411221128.qSNFAkty-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> fs/unicode/utf8-core.c:225: warning: Function parameter or struct member 'version_str' not described in 'utf8_parse_version'


vim +225 fs/unicode/utf8-core.c

9d53690f0d4e56 Gabriel Krisman Bertazi 2019-04-25  216  
142fa60f61f938 André Almeida           2024-10-21  217  /**
142fa60f61f938 André Almeida           2024-10-21  218   * utf8_parse_version - Parse a UTF-8 version number from a string
142fa60f61f938 André Almeida           2024-10-21  219   *
142fa60f61f938 André Almeida           2024-10-21  220   * @version: input string
142fa60f61f938 André Almeida           2024-10-21  221   *
fd9b2236cc1a2d guanjing                2024-11-20  222   * Returns 0 on success, negative code on error
142fa60f61f938 André Almeida           2024-10-21  223   */
fd9b2236cc1a2d guanjing                2024-11-20  224  int utf8_parse_version(char *version_str, unsigned int *version)
142fa60f61f938 André Almeida           2024-10-21 @225  {
diff mbox series

Patch

diff --git a/fs/unicode/utf8-core.c b/fs/unicode/utf8-core.c
index 6fc9ab8667e6..c54dc7ac5ce6 100644
--- a/fs/unicode/utf8-core.c
+++ b/fs/unicode/utf8-core.c
@@ -219,9 +219,9 @@  EXPORT_SYMBOL(utf8_unload);
  *
  * @version: input string
  *
- * Returns the parsed version on success, negative code on error
+ * Returns 0 on success, negative code on error
  */
-int utf8_parse_version(char *version)
+int utf8_parse_version(char *version_str, unsigned int *version)
 {
 	substring_t args[3];
 	unsigned int maj, min, rev;
@@ -230,13 +230,14 @@  int utf8_parse_version(char *version)
 		{0, NULL}
 	};
 
-	if (match_token(version, token, args) != 1)
+	if (match_token(version_str, token, args) != 1)
 		return -EINVAL;
 
 	if (match_int(&args[0], &maj) || match_int(&args[1], &min) ||
 	    match_int(&args[2], &rev))
 		return -EINVAL;
 
-	return UNICODE_AGE(maj, min, rev);
+	*version = UNICODE_AGE(maj, min, rev);
+	return 0;
 }
 EXPORT_SYMBOL(utf8_parse_version);
diff --git a/include/linux/unicode.h b/include/linux/unicode.h
index 5e6b212a2aed..7de545bc66cb 100644
--- a/include/linux/unicode.h
+++ b/include/linux/unicode.h
@@ -78,6 +78,6 @@  int utf8_casefold_hash(const struct unicode_map *um, const void *salt,
 struct unicode_map *utf8_load(unsigned int version);
 void utf8_unload(struct unicode_map *um);
 
-int utf8_parse_version(char *version);
+int utf8_parse_version(char *version_str, unsigned int *version);
 
 #endif /* _LINUX_UNICODE_H */
diff --git a/mm/shmem.c b/mm/shmem.c
index ccb9629a0f70..7d07f649a8df 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -4368,14 +4368,15 @@  static int shmem_parse_opt_casefold(struct fs_context *fc, struct fs_parameter *
 	unsigned int version = UTF8_LATEST;
 	struct unicode_map *encoding;
 	char *version_str = param->string + 5;
+	int ret;
 
 	if (!latest_version) {
 		if (strncmp(param->string, "utf8-", 5))
 			return invalfc(fc, "Only UTF-8 encodings are supported "
 				       "in the format: utf8-<version number>");
 
-		version = utf8_parse_version(version_str);
-		if (version < 0)
+		ret = utf8_parse_version(version_str, &version);
+		if (ret < 0)
 			return invalfc(fc, "Invalid UTF-8 version: %s", version_str);
 	}