[PATCH 1/6] libusbg: Remove fixed-size buffer from usbg_state.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Path length should not be placed in constant size buffer
but in allocated memory.

Signed-off-by: Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx>
---
 configure.ac |    1 +
 src/usbg.c   |   16 ++++++++++++----
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/configure.ac b/configure.ac
index 45449e2..878c2ab 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3,6 +3,7 @@ AM_INIT_AUTOMAKE([-Wall -Werror foreign])
 AC_PROG_CC
 AM_PROG_AR
 AC_CONFIG_MACRO_DIR([m4])
+AC_DEFINE([_GNU_SOURCE], [], [Use GNU extensions])
 LT_INIT
 AC_CONFIG_FILES([Makefile src/Makefile examples/Makefile libusbg.pc])
 DX_INIT_DOXYGEN([$PACKAGE_NAME],[doxygen.cfg])
diff --git a/src/usbg.c b/src/usbg.c
index f655675..95fca9a 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -37,7 +37,7 @@
 
 struct usbg_state
 {
-	char path[USBG_MAX_PATH_LENGTH];
+	char *path;
 
 	TAILQ_HEAD(ghead, usbg_gadget) gadgets;
 };
@@ -447,6 +447,8 @@ static void usbg_free_state(usbg_state *s)
 		TAILQ_REMOVE(&s->gadgets, g, gnode);
 		usbg_free_gadget(g);
 	}
+
+	free(s->path);
 	free(s);
 }
 
@@ -840,7 +842,8 @@ static int usbg_init_state(char *path, usbg_state *s)
 {
 	int ret = USBG_SUCCESS;
 
-	strcpy(s->path, path);
+	/* State takes the ownership of path and should free it */
+	s->path = path;
 	TAILQ_INIT(&s->gadgets);
 
 	ret = usbg_parse_gadgets(path, s);
@@ -858,9 +861,13 @@ int usbg_init(char *configfs_path, usbg_state **state)
 {
 	int ret = USBG_SUCCESS;
 	DIR *dir;
-	char path[USBG_MAX_PATH_LENGTH];
+	char *path;
 
-	sprintf(path, "%s/usb_gadget", configfs_path);
+	ret = asprintf(&path, "%s/usb_gadget", configfs_path);
+	if (ret < 0)
+		return USBG_ERROR_NO_MEM;
+	else
+		ret = USBG_SUCCESS;
 
 	/* Check if directory exist */
 	dir = opendir(path);
@@ -876,6 +883,7 @@ int usbg_init(char *configfs_path, usbg_state **state)
 	} else {
 		ERRORNO("couldn't init gadget state\n");
 		ret = usbg_translate_error(errno);
+		free(path);
 	}
 
 	return ret;
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux