change sscanf to strtol for getting fd 53/125453/1
authorJunkyeong Kim <jk0430.kim@samsung.com>
Mon, 17 Apr 2017 08:42:07 +0000 (17:42 +0900)
committerJunkyeong Kim <jk0430.kim@samsung.com>
Mon, 17 Apr 2017 08:42:12 +0000 (17:42 +0900)
Change-Id: I4e801b928c4960e66f868e374fc11ab93a55fbe8
Signed-off-by: Junkyeong Kim <jk0430.kim@samsung.com>
src/tbm_drm_helper_client.c
src/tbm_drm_helper_server.c

index 0ca1575..6611d64 100644 (file)
@@ -221,16 +221,38 @@ int
 tbm_drm_helper_get_fd(void)
 {
        const char *value;
-       int ret, flags, fd = -1;
+       int flags, fd = -1;
        int new_fd = -1;
+       char *end;
+       errno = 0;
 
        value = (const char*)getenv("TBM_DRM_FD");
        if (!value)
                return -1;
 
-       ret = sscanf(value, "%d", &fd);
-       if (ret <= 0)
+       const long sl = strtol(value, &end, 10);
+       if (end == value) {
+               TBM_LOG_E("%s: not a decimal number\n", value);
                return -1;
+       } else if (*end != '\0') {
+               TBM_LOG_E("%s: extra characters at end of input: %s\n", value, end);
+               return -1;
+       } else if ((sl == LONG_MIN || sl == LONG_MAX) && errno == ERANGE) {
+               TBM_LOG_E("%s out of range of type long\n", value);
+               return -1;
+       } else if (sl > INT_MAX) {
+               TBM_LOG_E("%ld greater than INT_MAX\n", sl);
+               return -1;
+       } else if (sl < INT_MIN) {
+               TBM_LOG_E("%ld less than INT_MIN\n", sl);
+               return -1;
+       } else {
+               fd = (int)sl;
+               if (fd < 0) {
+                       TBM_LOG_E("%d out of fd range\n", fd);
+                       return -1;
+               }
+       }
 
        TBM_LOG_I("TBM_DRM_FD: %d\n", fd);
 
index 75ab082..65a9c45 100644 (file)
@@ -196,16 +196,38 @@ int
 tbm_drm_helper_get_master_fd(void)
 {
        const char *value;
-       int ret, flags, fd = -1;
+       int flags, fd = -1;
        int new_fd = -1;
+       char *end;
+       errno = 0;
 
        value = (const char*)getenv("TDM_DRM_MASTER_FD");
        if (!value)
                return -1;
 
-       ret = sscanf(value, "%d", &fd);
-       if (ret <= 0)
+       const long sl = strtol(value, &end, 10);
+       if (end == value) {
+               TBM_LOG_E("%s: not a decimal number\n", value);
                return -1;
+       } else if (*end != '\0') {
+               TBM_LOG_E("%s: extra characters at end of input: %s\n", value, end);
+               return -1;
+       } else if ((sl == LONG_MIN || sl == LONG_MAX) && errno == ERANGE) {
+               TBM_LOG_E("%s out of range of type long\n", value);
+               return -1;
+       } else if (sl > INT_MAX) {
+               TBM_LOG_E("%ld greater than INT_MAX\n", sl);
+               return -1;
+       } else if (sl < INT_MIN) {
+               TBM_LOG_E("%ld less than INT_MIN\n", sl);
+               return -1;
+       } else {
+               fd = (int)sl;
+               if (fd < 0) {
+                       TBM_LOG_E("%d out of fd range\n", fd);
+                       return -1;
+               }
+       }
 
        TBM_LOG_I("TDM_DRM_MASTER_FD: %d\n", fd);