[igt-dev] [PATCH i-g-t 06/21] lib: Avoid memcpying 0 bytes from NULL

Petri Latvala
2019-01-16T11:21:03Z

The behaviour of memcpying 0 bytes from NULL is semantically sound,
but still undefined behaviour.

Signed-off-by: Petri Latvala <petri.latvala@intel.com>
Cc: Arkadiusz Hiler <arkadiusz.hiler@intel.com>
---
lib/igt_core.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/igt_core.c b/lib/igt_core.c
index 50d6008f..7c727edc 100644
--- a/lib/igt_core.c
+++ b/lib/igt_core.c
@@ -728,8 +728,9 @@ static int common_init(int *argc, char **argv,
all_opt_count += ARRAY_SIZE(long_options);

combined_opts = malloc(all_opt_count * sizeof(*combined_opts));
- memcpy(combined_opts, extra_long_opts,
- extra_opt_count * sizeof(*combined_opts));
+ if (extra_opt_count > 0)
+ memcpy(combined_opts, extra_long_opts,
+ extra_opt_count * sizeof(*combined_opts));

/* Copy the subtest long options (and the final NULL entry) */
memcpy(&combined_opts[extra_opt_count], long_options,
--
2.19.1