Skip to content

Commit d65360f

Browse files
chaseyutehcaster
authored andcommitted
mm/slub: clean up create_unique_id()
As Christophe JAILLET suggested [1] In create_unique_id(), "looks that ID_STR_LENGTH could even be reduced to 32 or 16. The 2nd BUG_ON at the end of the function could certainly be just removed as well or remplaced by a: if (p > name + ID_STR_LENGTH - 1) { kfree(name); return -E<something>; } " According to above suggestion, let's do below cleanups: 1. reduce ID_STR_LENGTH to 32, as the buffer size should be enough; 2. use WARN_ON instead of BUG_ON() and return error if check condition is true; 3. use snprintf instead of sprintf to avoid overflow. [1] https://lore.kernel.org/linux-mm/[email protected]/ Suggested-by: Christophe JAILLET <[email protected]> Reviewed-by: Hyeonggon Yoo <[email protected]> Signed-off-by: Chao Yu <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]>
1 parent 2bfbb02 commit d65360f

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

mm/slub.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5874,7 +5874,7 @@ static inline struct kset *cache_kset(struct kmem_cache *s)
58745874
return slab_kset;
58755875
}
58765876

5877-
#define ID_STR_LENGTH 64
5877+
#define ID_STR_LENGTH 32
58785878

58795879
/* Create a unique string id for a slab cache:
58805880
*
@@ -5907,9 +5907,12 @@ static char *create_unique_id(struct kmem_cache *s)
59075907
*p++ = 'A';
59085908
if (p != name + 1)
59095909
*p++ = '-';
5910-
p += sprintf(p, "%07u", s->size);
5910+
p += snprintf(p, ID_STR_LENGTH - (p - name), "%07u", s->size);
59115911

5912-
BUG_ON(p > name + ID_STR_LENGTH - 1);
5912+
if (WARN_ON(p > name + ID_STR_LENGTH - 1)) {
5913+
kfree(name);
5914+
return ERR_PTR(-EINVAL);
5915+
}
59135916
return name;
59145917
}
59155918

0 commit comments

Comments
 (0)