change sscanf to strtol for getting fd 54/125454/1
authorJunkyeong Kim <jk0430.kim@samsung.com>
Mon, 17 Apr 2017 08:43:42 +0000 (17:43 +0900)
committerJunkyeong Kim <jk0430.kim@samsung.com>
Mon, 17 Apr 2017 08:43:47 +0000 (17:43 +0900)
Change-Id: I7fed3efbd3fc9a9273a670b3b967707453471be2
Signed-off-by: Junkyeong Kim <jk0430.kim@samsung.com>
src/tdm_helper.c

index 6b4178e..4adbcb8 100644 (file)
@@ -521,15 +521,35 @@ tdm_helper_get_fd(const char *env)
 {
        const char *value;
        int fd, newfd, flags, ret;
+       char *end;
+       errno = 0;
 
        value = (const char*)getenv(env);
        if (!value)
                return -1;
 
-       ret = sscanf(value, "%d", &fd);
-       if (ret < 0) {
-               TDM_ERR("sscanf failed: %m");
+       const long sl = strtol(value, &end, 10);
+       if (end == value) {
+               TDM_ERR("%s: not a decimal number\n", value);
                return -1;
+       } else if (*end != '\0') {
+               TDM_ERR("%s: extra characters at end of input: %s\n", value, end);
+               return -1;
+       } else if ((sl == LONG_MIN || sl == LONG_MAX) && errno == ERANGE) {
+               TDM_ERR("%s out of range of type long\n", value);
+               return -1;
+       } else if (sl > INT_MAX) {
+               TDM_ERR("%ld greater than INT_MAX\n", sl);
+               return -1;
+       } else if (sl < INT_MIN) {
+               TDM_ERR("%ld less than INT_MIN\n", sl);
+               return -1;
+       } else {
+               fd = (int)sl;
+               if (fd < 0) {
+                       TDM_ERR("%d out of fd range\n", fd);
+                       return -1;
+               }
        }
 
        flags = fcntl(fd, F_GETFD);