1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-26 03:22:00 +03:00

udev: udev_event_apply_format() always make buf NUL terminated

The return value of udev_event_apply_format() is always ignored.
So, the destination buffer must be always NUL terminated.
This commit is contained in:
Yu Watanabe 2020-06-18 13:48:45 +09:00
parent bc568a7a35
commit 5eb6ef8b45
3 changed files with 17 additions and 14 deletions

View File

@ -437,9 +437,9 @@ null_terminate:
return 0; return 0;
} }
ssize_t udev_event_apply_format(UdevEvent *event, size_t udev_event_apply_format(UdevEvent *event,
const char *src, char *dest, size_t size, const char *src, char *dest, size_t size,
bool replace_whitespace) { bool replace_whitespace) {
const char *s = src; const char *s = src;
int r; int r;
@ -455,9 +455,10 @@ ssize_t udev_event_apply_format(UdevEvent *event,
ssize_t subst_len; ssize_t subst_len;
r = get_subst_type(&s, false, &type, attr); r = get_subst_type(&s, false, &type, attr);
if (r < 0) if (r < 0) {
return log_device_warning_errno(event->dev, r, "Invalid format string, ignoring: %s", src); log_device_warning_errno(event->dev, r, "Invalid format string, ignoring: %s", src);
if (r == 0) { break;
} else if (r == 0) {
if (size < 2) /* need space for this char and the terminating NUL */ if (size < 2) /* need space for this char and the terminating NUL */
break; break;
*dest++ = *s++; *dest++ = *s++;
@ -466,10 +467,12 @@ ssize_t udev_event_apply_format(UdevEvent *event,
} }
subst_len = udev_event_subst_format(event, type, attr, dest, size); subst_len = udev_event_subst_format(event, type, attr, dest, size);
if (subst_len < 0) if (subst_len < 0) {
return log_device_warning_errno(event->dev, subst_len, log_device_warning_errno(event->dev, subst_len,
"Failed to substitute variable '$%s' or apply format '%%%c', ignoring: %m", "Failed to substitute variable '$%s' or apply format '%%%c', ignoring: %m",
format_type_to_string(type), format_type_to_char(type)); format_type_to_string(type), format_type_to_char(type));
break;
}
/* FORMAT_SUBST_RESULT handles spaces itself */ /* FORMAT_SUBST_RESULT handles spaces itself */
if (replace_whitespace && type != FORMAT_SUBST_RESULT) if (replace_whitespace && type != FORMAT_SUBST_RESULT)

View File

@ -48,9 +48,9 @@ UdevEvent *udev_event_new(sd_device *dev, usec_t exec_delay_usec, sd_netlink *rt
UdevEvent *udev_event_free(UdevEvent *event); UdevEvent *udev_event_free(UdevEvent *event);
DEFINE_TRIVIAL_CLEANUP_FUNC(UdevEvent*, udev_event_free); DEFINE_TRIVIAL_CLEANUP_FUNC(UdevEvent*, udev_event_free);
ssize_t udev_event_apply_format(UdevEvent *event, size_t udev_event_apply_format(UdevEvent *event,
const char *src, char *dest, size_t size, const char *src, char *dest, size_t size,
bool replace_whitespace); bool replace_whitespace);
int udev_check_format(const char *value, size_t *offset, const char **hint); int udev_check_format(const char *value, size_t *offset, const char **hint);
int udev_event_spawn(UdevEvent *event, int udev_event_spawn(UdevEvent *event,
usec_t timeout_usec, usec_t timeout_usec,

View File

@ -151,7 +151,7 @@ int test_main(int argc, char *argv[], void *userdata) {
ORDERED_HASHMAP_FOREACH_KEY(val, cmd, event->run_list, i) { ORDERED_HASHMAP_FOREACH_KEY(val, cmd, event->run_list, i) {
char program[UTIL_PATH_SIZE]; char program[UTIL_PATH_SIZE];
udev_event_apply_format(event, cmd, program, sizeof(program), false); (void) udev_event_apply_format(event, cmd, program, sizeof(program), false);
printf("run: '%s'\n", program); printf("run: '%s'\n", program);
} }