Fix cppcheck warning "Non reentrant function 'readdir' called"

For threadsafe applications it is recommended to use the reentrant replacement function 'readdir_r'.
This commit is contained in:
Philippe Teuwen 2013-03-06 15:20:15 +01:00
parent 4dafd70b64
commit a0b859b2a1

View file

@ -39,6 +39,7 @@
#include <stdio.h> #include <stdio.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include "nfc-internal.h" #include "nfc-internal.h"
@ -337,26 +338,32 @@ uart_list_ports(void)
size_t szRes = 1; size_t szRes = 1;
res[0] = NULL; res[0] = NULL;
DIR *dir;
DIR *pdDir = opendir("/dev"); if ((dir = opendir("/dev")) == NULL) {
struct dirent *pdDirEnt; perror("opendir error: /dev");
while ((pdDirEnt = readdir(pdDir)) != NULL) { return res;
}
struct dirent entry;
struct dirent *result;
while ((readdir_r(dir, &entry, &result) == 0) && (result != NULL)) {
#if !defined(__APPLE__) #if !defined(__APPLE__)
if (!isdigit(pdDirEnt->d_name[strlen(pdDirEnt->d_name) - 1])) if (!isdigit(entry.d_name[strlen(entry.d_name) - 1]))
continue; continue;
#endif #endif
const char **p = serial_ports_device_radix; const char **p = serial_ports_device_radix;
while (*p) { while (*p) {
if (!strncmp(pdDirEnt->d_name, *p, strlen(*p))) { if (!strncmp(entry.d_name, *p, strlen(*p))) {
char **res2 = realloc(res, (szRes + 1) * sizeof(char *)); char **res2 = realloc(res, (szRes + 1) * sizeof(char *));
if (!res2) if (!res2) {
perror("malloc");
goto oom; goto oom;
}
res = res2; res = res2;
if (!(res[szRes - 1] = malloc(6 + strlen(pdDirEnt->d_name)))) if (!(res[szRes - 1] = malloc(6 + strlen(entry.d_name)))) {
perror("malloc");
goto oom; goto oom;
}
sprintf(res[szRes - 1], "/dev/%s", pdDirEnt->d_name); sprintf(res[szRes - 1], "/dev/%s", entry.d_name);
szRes++; szRes++;
res[szRes - 1] = NULL; res[szRes - 1] = NULL;
@ -365,7 +372,7 @@ uart_list_ports(void)
} }
} }
oom: oom:
closedir(pdDir); closedir(dir);
return res; return res;
} }