From 914d79c980ee5c33561d5b6a1c7556974527a701 Mon Sep 17 00:00:00 2001 From: Mark Pizzolato Date: Tue, 8 May 2018 22:48:12 -0700 Subject: [PATCH] SERIAL: Avoid potential SEGFAULT when scandir() fails on Linux --- sim_serial.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sim_serial.c b/sim_serial.c index 613f622d..1b70ed3b 100644 --- a/sim_serial.c +++ b/sim_serial.c @@ -952,12 +952,12 @@ int ports = 0; memset(list, 0, max*sizeof(*list)); #if defined(__linux) || defined(__linux__) if (1) { - struct dirent **namelist; + struct dirent **namelist = NULL; struct stat st; i = scandir("/sys/class/tty/", &namelist, NULL, NULL); - while (i--) { + while (0 < i--) { if (strcmp(namelist[i]->d_name, ".") && strcmp(namelist[i]->d_name, "..")) { char path[1024], devicepath[1024], driverpath[1024];