Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rtc: sysfs: Bail out earlier if no new groups provided

When there is no new groups provided, no need to reallocate memory,
copy the old ones and free them in order to do nothing. Do nothing
instead.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20250702073224.2684097-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>

authored by

Andy Shevchenko and committed by
Alexandre Belloni
bbe8d4fe 4dda8df7

+8 -4
+8 -4
drivers/rtc/sysfs.c
··· 314 314 size_t old_cnt = 0, add_cnt = 0, new_cnt; 315 315 const struct attribute_group **groups, **old; 316 316 317 - if (!grps) 317 + if (grps) { 318 + for (groups = grps; *groups; groups++) 319 + add_cnt++; 320 + /* No need to modify current groups if nothing new is provided */ 321 + if (add_cnt == 0) 322 + return 0; 323 + } else { 318 324 return -EINVAL; 325 + } 319 326 320 327 groups = rtc->dev.groups; 321 328 if (groups) 322 329 for (; *groups; groups++) 323 330 old_cnt++; 324 - 325 - for (groups = grps; *groups; groups++) 326 - add_cnt++; 327 331 328 332 new_cnt = old_cnt + add_cnt + 1; 329 333 groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL);