diff options
author | Li Xiaoming <lixm.fnst@cn.fujitsu.com> | 2019-08-21 16:53:37 +0800 |
---|---|---|
committer | Jan-Simon Moeller <jsmoeller@linuxfoundation.org> | 2019-08-22 11:16:16 +0000 |
commit | 81006a03b9014ec482200a7fedb189b97abbff4a (patch) | |
tree | a3faa8c3bedbc4ed2a7065cbc13a389549734b89 | |
parent | 8310076d9255f0a54d6b9c0064b3e08469970497 (diff) |
Remove potential overflow risk & improve indent
The patch below does:
1) As d_name type may hold 255(NAME_MAX) characters at most,
so I enlarge the array length to include the proper size.
maxX and maxY are s32(int32) type(-2,147,483,648 ~ +2,147,483,647),
which will cost 11 characters at most when writing to the arrary.
Also silence the warning.
2) Replace sprintf with snprintf, a safer version;
3) Improve indent;
4) Remove debug sentence.
Bug-AGL: SPEC-2422
Change-Id: I6d83a92a3138decd2bc9b750b9659d581a9f1b6b
Signed-off-by: Li Xiaoming <lixm.fnst@cn.fujitsu.com>
-rw-r--r-- | tsutils/tsutils.c | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/tsutils/tsutils.c b/tsutils/tsutils.c index 79385d1..125e975 100644 --- a/tsutils/tsutils.c +++ b/tsutils/tsutils.c @@ -19,40 +19,41 @@ #include <fcntl.h> #include <stdio.h> #include <string.h> +#include <limits.h> #define LONG_BITS (sizeof(long) << 3) #define NUM_LONGS(bits) (((bits) + LONG_BITS - 1) / LONG_BITS) #define DEV_INPUT "/dev/input" +#define LENGTH_TSDEVNODE (NAME_MAX + 1 + sizeof(DEV_INPUT)) +#define LENGTH_TSINFO (LENGTH_TSDEVNODE + 2 + 11*2) -char tsInfo[200]; +char tsInfo[LENGTH_TSINFO]; static inline int testBit(long bit, const long *array) { return (array[bit / LONG_BITS] >> bit % LONG_BITS) & 1; } char* getTouchScreenInfo() { - DIR* dir; + DIR* dir; struct dirent* itemPtr; int fd; int isSingleTouch = 0, hasTouchScreen = 0; long absbits[NUM_LONGS(ABS_CNT)]; - char tsDevNode[100]; + char tsDevNode[LENGTH_TSDEVNODE]; struct input_absinfo absInfo; + int maxX, maxY; - int maxX, maxY; + memset(tsDevNode, 0, sizeof(tsDevNode)); + memset(tsInfo, 0, sizeof(tsInfo)); - memset(tsDevNode, 0, sizeof(tsDevNode)); - memset(tsInfo, 0, sizeof(tsInfo)); - - if ((dir = opendir(DEV_INPUT)) == NULL) { + if ((dir = opendir(DEV_INPUT)) == NULL) { printf("open %s failed.\n", DEV_INPUT); return NULL; } - while ((itemPtr = readdir(dir)) != NULL) { - //printf("name : %s---type : %d\n", itemPtr->d_name, itemPtr->d_type); + while ((itemPtr = readdir(dir)) != NULL) { if ((strstr(itemPtr->d_name, "event") != NULL) && (2 == itemPtr->d_type)) { - sprintf(tsDevNode, "%s/%s", DEV_INPUT, itemPtr->d_name); + snprintf(tsDevNode, sizeof(tsDevNode), "%s/%s", DEV_INPUT, itemPtr->d_name); fd = open(tsDevNode, O_RDONLY); if (fd < 0) { printf("open %s failed.\n", tsDevNode); @@ -81,15 +82,17 @@ char* getTouchScreenInfo() { } hasTouchScreen = 1; - break; + break; } } - if (!hasTouchScreen) return NULL; + if (!hasTouchScreen) { + return NULL; + } - sprintf(tsInfo, "%s %d %d", tsDevNode, maxX, maxY); + snprintf(tsInfo, sizeof(tsInfo), "%s %d %d", tsDevNode, maxX, maxY); - close(fd); - closedir(dir); - return tsInfo; + close(fd); + closedir(dir); + return tsInfo; } |