libion: Coverity Issue Check [1/1]
PD#SWPL-64188
Problem:
coverity issue check
Solution:
fix coverity defects
Verify:
t7-an400
Change-Id: I422691b1ad21fb9825de7d6c4b4e91d169d7755e
Signed-off-by: Yongjie Zhu <yongjie.zhu@amlogic.com>
diff --git a/IONmem.c b/IONmem.c
index 1923451..65697ea 100644
--- a/IONmem.c
+++ b/IONmem.c
@@ -36,7 +36,7 @@
ion_fd = ion_open();
if (ion_fd < 0) {
- __E("%s failed: '%s'\n", strerror(errno));
+ __E("%s failed: '%s'\n", __func__, strerror(errno));
return -1;
}
__D("%s, ion_fd=%d\n", __func__, ion_fd);
@@ -46,41 +46,54 @@
int ion_mem_alloc_fd(int ion_fd, size_t size, IONMEM_AllocParams *params, unsigned int flag, unsigned int alloc_hmask)
{
- int ret = -1;
- int i = 0, num_heaps = 0;
+ int ret = 0;
+ int num_heaps = 0;
unsigned int heap_mask = 0;
- if (ion_query_heap_cnt(ion_fd, &num_heaps) >= 0) {
- __D("num_heaps=%d\n", num_heaps);
- struct ion_heap_data *const heaps = (struct ion_heap_data *)malloc(num_heaps * sizeof(struct ion_heap_data));
- if (heaps != NULL && num_heaps) {
- if (ion_query_get_heaps(ion_fd, num_heaps, heaps) >= 0) {
- for (int i = 0; i != num_heaps; ++i) {
- __D("heaps[%d].type=%d, heap_id=%d\n", i, heaps[i].type, heaps[i].heap_id);
- if ((1 << heaps[i].type) == alloc_hmask) {
- heap_mask = 1 << heaps[i].heap_id;
- __D("%d, m=%x, 1<<heap_id=%x, heap_mask=%x, name=%s, alloc_hmask=%x\n",
- heaps[i].type, 1<<heaps[i].type, heaps[i].heap_id, heap_mask, heaps[i].name, alloc_hmask);
- break;
- }
- }
- }
- free(heaps);
- if (heap_mask)
- ret = ion_alloc_fd(ion_fd, size, 0, heap_mask, flag, ¶ms->mImageFd);
- else
- __E("don't find match heap!!\n");
- } else {
- __E("heaps is NULL or no heaps,num_heaps=%d\n", num_heaps);
- }
- } else {
- __E("query_heap_cnt fail! no ion heaps for alloc!!!\n");
+ ret = ion_query_heap_cnt(ion_fd, &num_heaps);
+ if (ret < 0) {
+ __E("ion_query_heap_cnt fail! no ion heaps for alloc!!!\n");
+ return -ENOMEM;
}
+
+ __D("num_heaps=%d\n", num_heaps);
+ struct ion_heap_data *const heaps = (struct ion_heap_data *)malloc(num_heaps * sizeof(struct ion_heap_data));
+ if (num_heaps <= 0 || heaps == NULL) {
+ free(heaps);
+ __E("heaps is NULL or no heaps,num_heaps=%d\n", num_heaps);
+ return -ENOMEM;
+ }
+
+ ret = ion_query_get_heaps(ion_fd, num_heaps, heaps);
+ if (ret < 0) {
+ free(heaps);
+ __E("ion_query_get_heaps fail! no ion heaps for alloc!!!\n");
+ return -ENOMEM;
+ }
+
+ for (int i = 0; i != num_heaps; ++i) {
+ __D("heaps[%d].type=%d, heap_id=%d\n", i, heaps[i].type, heaps[i].heap_id);
+ if ((1 << heaps[i].type) == alloc_hmask) {
+ heap_mask = 1 << heaps[i].heap_id;
+ __D("%d, m=%x, 1<<heap_id=%x, heap_mask=%x, name=%s, alloc_hmask=%x\n",
+ heaps[i].type, 1<<heaps[i].type, heaps[i].heap_id, heap_mask, heaps[i].name, alloc_hmask);
+ break;
+ }
+ }
+
+ free(heaps);
+ if (heap_mask == 0) {
+ __E("don't find match heap!!\n");
+ return -ENOMEM;
+ }
+
+ ret = ion_alloc_fd(ion_fd, size, 0, heap_mask, flag, ¶ms->mImageFd);
if (ret < 0) {
__E("ion_alloc failed, errno=%d\n", errno);
return -ENOMEM;
}
- return ret;
+
+ return 0;
}
unsigned long ion_mem_alloc(int ion_fd, size_t size, IONMEM_AllocParams *params, bool cache_flag)
@@ -189,7 +202,7 @@
ret = ion_close(ion_fd);
if (ret < 0) {
- __E("%s err (%s)! ion_fd=%d\n", strerror(errno), ion_fd);
+ __E("%s err (%s)! ion_fd=%d\n", __func__, strerror(errno), ion_fd);
return;
}
}
diff --git a/ion.c b/ion.c
index a616071..7799e2b 100644
--- a/ion.c
+++ b/ion.c
@@ -57,23 +57,25 @@
if (fd < 0) {
ALOGE("%s open failed\n", VERSION_FILE);
return -1;
+ } else {
+ ret = read(fd, version_str, 128);
+ if (ret < 0) {
+ close(fd);
+ ALOGE("%s read failed\n", VERSION_FILE);
+ return -1;
+ }
}
- ret = read(fd, version_str, 128);
- if (ret < 0) {
- ALOGE("%s read failed\n", VERSION_FILE);
- return -1;
- }
-
- close(fd);
ret = sscanf((const char *)version_str,
"Linux version %d.%d.%d", &version,
&patchlevel, &sublevel);
if (ret != 3) {
+ close(fd);
ALOGE("sscanf error\n");
return -1;
}
+ close(fd);
return KERNEL_VERSION(version, patchlevel, sublevel);
}
diff --git a/ion_test.c b/ion_test.c
index b2c46ed..894cc7b 100644
--- a/ion_test.c
+++ b/ion_test.c
@@ -48,42 +48,46 @@
if (ion_fd < 0) {
printf("ion_mem_init failed\n");
return ion_fd;
+ } else {
+ ret = ion_mem_alloc(ion_fd, len, ¶ms, alloc_flags);
+ ion_close(ion_fd);
+ if (ret)
+ printf("%s failed: %s\n", __func__, strerror(ret));
+ else
+ printf("%s: passed\n", __func__);
+ return ret;
}
- ret = ion_mem_alloc(ion_fd, len, ¶ms, alloc_flags);
- if (ret)
- printf("%s failed: %s\n", __func__, strerror(ret));
- else
- printf("%s: passed\n", __func__);
- return ret;
}
-int _ion_alloc_test(int *fd, ion_user_handle_t *handle)
+int _ion_alloc_test(int fd, ion_user_handle_t *handle)
{
int ret;
int legacy_ion = 0;
- *fd = ion_open();
- if (*fd < 0)
- return *fd;
+ fd = ion_open();
+ if (fd < 0)
+ return fd;
- legacy_ion = ion_is_legacy(*fd);
+ legacy_ion = ion_is_legacy(fd);
printf("legacy_ion=%d\n", legacy_ion);
if (legacy_ion)
- ret = ion_alloc(*fd, len, align, heap_mask, alloc_flags, handle);
+ ret = ion_alloc(fd, len, align, heap_mask, alloc_flags, handle);
else
- ret = ion_alloc_fd(*fd, len, align, heap_mask, alloc_flags, handle);
+ ret = ion_alloc_fd(fd, len, align, heap_mask, alloc_flags, handle);
- if (ret)
- printf("%s failed: %s\n", __func__, strerror(ret));
+ ion_close(fd);
+ if (ret < 0)
+ printf("failed alloc dma buffer\n");
return ret;
}
void ion_alloc_test()
{
- int fd, ret = 0;
+ int fd = 0;
+ int ret = 0;
ion_user_handle_t handle;
- if (_ion_alloc_test(&fd, &handle))
+ if (_ion_alloc_test(fd, &handle))
return;
if (ion_is_legacy(fd))
@@ -92,23 +96,27 @@
printf("%s failed: %s %d\n", __func__, strerror(ret), handle);
return;
}
+
ion_close(fd);
printf("ion alloc test: passed\n");
}
void ion_map_test()
{
- int fd, map_fd, ret;
+ int fd = 0;
+ int map_fd, ret;
size_t i;
ion_user_handle_t handle;
unsigned char *ptr;
- if (_ion_alloc_test(&fd, &handle))
+ if (_ion_alloc_test(fd, &handle))
return;
ret = ion_map(fd, handle, len, prot, map_flags, 0, &ptr, &map_fd);
- if (ret)
+ if (ret) {
+ munmap(ptr, len);
return;
+ }
for (i = 0; i < len; i++) {
ptr[i] = (unsigned char)i;
@@ -123,9 +131,6 @@
munmap(ptr, len);
close(map_fd);
- _ion_alloc_test(&fd, &handle);
- close(fd);
-
#if 0
munmap(ptr, len);
close(map_fd);
@@ -159,16 +164,18 @@
};
struct cmsghdr *cmsg;
- int fd, share_fd, ret;
+ int fd = 0;
+ int share_fd, ret;
char *ptr;
/* parent */
- if (_ion_alloc_test(&fd, &handle))
+ if (_ion_alloc_test(fd, &handle))
return;
ret = ion_share(fd, handle, &share_fd);
if (ret)
printf("share failed %s\n", strerror(errno));
ptr = mmap(NULL, len, prot, map_flags, share_fd, 0);
if (ptr == MAP_FAILED) {
+ close(fd);
return;
}
strcpy(ptr, "master");
@@ -180,13 +187,15 @@
/* send the fd */
printf("master? [%10s] should be [master]\n", ptr);
printf("master sending msg 1\n");
- sendmsg(sd[0], &msg, 0);
+ if (sendmsg(sd[0], &msg, 0) < 0)
+ perror("master send msg 2");
if (recvmsg(sd[0], &msg, 0) < 0)
perror("master recv msg 2");
printf("master? [%10s] should be [child]\n", ptr);
/* send ping */
- sendmsg(sd[0], &msg, 0);
+ if (sendmsg(sd[0], &msg, 0) < 0)
+ perror("master send msg 1");
printf("master->master? [%10s]\n", ptr);
if (recvmsg(sd[0], &msg, 0) < 0)
perror("master recv 1");
@@ -224,15 +233,20 @@
}
printf("child %d\n", recv_fd);
fd = ion_open();
+ if (fd < 0)
+ return fd;
ptr = mmap(NULL, len, prot, map_flags, recv_fd, 0);
if (ptr == MAP_FAILED) {
+ close(fd);
return;
}
printf("child? [%10s] should be [master]\n", ptr);
strcpy(ptr, "child");
printf("child sending msg 2\n");
- sendmsg(sd[1], &child_msg, 0);
+ if (sendmsg(sd[1], &child_msg, 0) < 0)
+ perror("master send msg 2");
close(fd);
+ _exit(0);
}
}