X-Git-Url: http://git.liburcu.org/?a=blobdiff_plain;f=src%2Fcommon%2Fsmp.c;h=980947cf4de9431880494a0d55914811ec974bd0;hb=feac72042ef1572189d4280a4f151ce049ac2b1e;hp=a4346591c4595c0682e757ed700c724f6c74b99b;hpb=66dbdc3448a77043d0fd59f47b17e77a8d59fddb;p=lttng-ust.git diff --git a/src/common/smp.c b/src/common/smp.c index a4346591..980947cf 100644 --- a/src/common/smp.c +++ b/src/common/smp.c @@ -10,13 +10,13 @@ #include #include #include +#include #include #include #include #include -#include "common/align.h" #include "common/logging.h" #include "common/smp.h" @@ -105,14 +105,14 @@ int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) { ssize_t bytes_read = 0; size_t total_bytes_read = 0; - int fd = 0; + int fd = -1, ret = -1; if (buf == NULL) - return -1; + goto end; fd = open("/sys/devices/system/cpu/possible", O_RDONLY); if (fd < 0) - return -1; + goto end; do { bytes_read = read(fd, buf + total_bytes_read, @@ -122,7 +122,7 @@ int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) if (errno == EINTR) { continue; /* retry operation */ } else { - return -1; + goto end; } } @@ -130,9 +130,6 @@ int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) assert(total_bytes_read <= max_bytes); } while (max_bytes > total_bytes_read && bytes_read > 0); - if (close(fd)) - PERROR("close"); - /* * Make sure the mask read is a null terminated string. */ @@ -141,7 +138,13 @@ int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) else buf[max_bytes - 1] = '\0'; - return total_bytes_read; + if (total_bytes_read > INT_MAX) + goto end; + ret = (int) total_bytes_read; +end: + if (fd >= 0 && close(fd) < 0) + PERROR("close"); + return ret; } /* @@ -187,11 +190,10 @@ error: static void _get_num_possible_cpus(void) { int ret; - int buf_len = LTTNG_UST_PAGE_SIZE; - char buf[buf_len]; + char buf[LTTNG_UST_CPUMASK_SIZE]; /* Get the possible cpu mask from sysfs, fallback to sysconf. */ - ret = get_possible_cpu_mask_from_sysfs((char *) &buf, buf_len); + ret = get_possible_cpu_mask_from_sysfs((char *) &buf, LTTNG_UST_CPUMASK_SIZE); if (ret <= 0) goto fallback;