This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TDA4VH-Q1: Memory leak in rpmsg_char.c

Part Number: TDA4VH-Q1

Tool/software:

Dear TI Forum,

we found a bug in the library rpmsg library.

Here is a patch with a possible fix:

# * TI RPMsg Char Utility Library
# *
# * Utility Library for accessing rpmsg devices through rpmsg-char driver
# *
# * Copyright (C) 2020 Texas Instruments Incorporated - https://www.ti.com/
# *
# * Redistribution and use in source and binary forms, with or without
# * modification, are permitted provided that the following conditions
# * are met:
# *
# * Redistributions of source code must retain the above copyright
# * notice, this list of conditions and the following disclaimer.
# *
# * Redistributions in binary form must reproduce the above copyright
# * notice, this list of conditions and the following disclaimer in the
# * documentation and/or other materials provided with the distribution.
# *
# * Neither the name of Texas Instruments Incorporated nor the names of
# * its contributors may be used to endorse or promote products derived
# * from this software without specific prior written permission.
# *
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# *
# */

diff --git a/src/rpmsg_char.c b/src/rpmsg_char.c
index afb4fec..b2886b7 100644
--- a/src/rpmsg_char.c
+++ b/src/rpmsg_char.c
@@ -274,34 +274,29 @@ static int _rpmsg_char_find_ctrldev(struct rpmsg_char_endpt *ept,
 	 * virtio device number and the rpmsg device number.
 	 */
 	sprintf(ctrl_path, "virtio%u.rpmsg_ctrl.0.0", ept->virtio_id);
-	dir = opendir(rpath);
-	if (dir) {
-		/*
-		 * find a directory that matches the rpmsg_ctrl
-		 * form virtio%d.rpmsg_ctrl.0.0
-		 */
-		while ((iter = readdir(dir))) {
-			if (iter->d_type == DT_DIR) {
-				if (!strcmp(iter->d_name, ctrl_path))
-					break;
-			}
+	rewinddir(dir);
+	/*
+		* find a directory that matches the rpmsg_ctrl
+		* form virtio%d.rpmsg_ctrl.0.0
+		*/
+	while ((iter = readdir(dir))) {
+		if (iter->d_type == DT_DIR) {
+			if (!strcmp(iter->d_name, ctrl_path))
+				break;
 		}
-		if (iter) {
-			memset(&fpath, 0, sizeof(fpath));
-			sprintf(fpath, "%s/%s/rpmsg", rpath, iter->d_name);
-			if (check_dir(fpath)) {
-				fprintf(stderr, "%s: rpmsg directory doesn't exist under %s\n",
-					__func__, ept->rpmsg_dev_name);
-				ret = -ENOENT;
-			} else {
-				ret = get_child_dir_suffix(fpath, "rpmsg_ctrl%u", &ctrl_id);
-			}
-		} else {
+	}
+	if (iter) {
+		memset(&fpath, 0, sizeof(fpath));
+		sprintf(fpath, "%s/%s/rpmsg", rpath, iter->d_name);
+		if (check_dir(fpath)) {
+			fprintf(stderr, "%s: rpmsg directory doesn't exist under %s\n",
+				__func__, ept->rpmsg_dev_name);
 			ret = -ENOENT;
+		} else {
+			ret = get_child_dir_suffix(fpath, "rpmsg_ctrl%u", &ctrl_id);
 		}
-	}
-	else  {
-		ret = -errno;
+	} else {
+		ret = -ENOENT;
 	}

 	/* check for backward compatibility if failed */

Regards

igmogo_ku