On Tue, Jun 7, 2011 at 5:19 PM, Andreas Fritiofson <
andreas.fritiof...@gmail.com> wrote:

>
> I suggest to drop this patch and merge a simpler fix for the problem
> adressed by this patch. Only a few lines of code would need to change.
>
>
Attached is such a patch. The benefit over Tomek's patch is that it frees
not only the allocated buffer but also closes the ftdi handle (as was done
in the original patch). Plus I'm not very fond of the
"infinite-loop-that's-not-actually-a-loop-at-all" along with the extra
indentation. This patch doesn't use gotos either but another (perhaps even
more) neglected construct, like it or not. :)

/Andreas
From c934a6c697c90fd5b5211f744e6607ae9cb7b848 Mon Sep 17 00:00:00 2001
From: Andreas Fritiofson <andreas.fritiof...@gmail.com>
Date: Wed, 8 Jun 2011 23:58:51 +0200
Subject: [PATCH] ft2232: clean up on error in ft2232_init()

Check malloc return value.
Close handle and free buffer if init fails late. Reuse ft2232_quit() for
this so move it up to avoid a forward declaration.
Don't set retval if it's not used.

Signed-off-by: Andreas Fritiofson <andreas.fritiof...@gmail.com>
---
 src/jtag/drivers/ft2232.c |   53 ++++++++++++++++++++++++--------------------
 1 files changed, 29 insertions(+), 24 deletions(-)

diff --git a/src/jtag/drivers/ft2232.c b/src/jtag/drivers/ft2232.c
index 8c2382a..a3b87c3 100644
--- a/src/jtag/drivers/ft2232.c
+++ b/src/jtag/drivers/ft2232.c
@@ -2418,6 +2418,25 @@ static int ft2232_set_data_bits_high_byte( uint8_t value, uint8_t direction )
 	return ERROR_OK;
 }
 
+static int ft2232_quit(void)
+{
+#if BUILD_FT2232_FTD2XX == 1
+	FT_STATUS status;
+
+	status = FT_Close(ftdih);
+#elif BUILD_FT2232_LIBFTDI == 1
+	ftdi_usb_close(&ftdic);
+
+	ftdi_deinit(&ftdic);
+#endif
+
+	if (ft2232_buffer)
+		free(ft2232_buffer);
+	ft2232_buffer = NULL;
+
+	return ERROR_OK;
+}
+
 static int ft2232_init(void)
 {
 	uint8_t  buf[1];
@@ -2467,9 +2486,11 @@ static int ft2232_init(void)
 
 	ft2232_buffer_size = 0;
 	ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
+	if (!ft2232_buffer)
+		return ft2232_quit(), ERROR_JTAG_INIT_FAILED;
 
 	if (layout->init() != ERROR_OK)
-		return ERROR_JTAG_INIT_FAILED;
+		return ft2232_quit(), ERROR_JTAG_INIT_FAILED;
 
 	if (ft2232_device_is_highspeed())
 	{
@@ -2482,21 +2503,23 @@ static int ft2232_init(void)
 #endif
 		/* make sure the legacy mode is disabled */
 		if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
-			return ERROR_JTAG_INIT_FAILED;
+			return ft2232_quit(), ERROR_JTAG_INIT_FAILED;
 	}
 
 	buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
-	if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
+	if (ft2232_write(buf, 1, &bytes_written) != ERROR_OK)
 	{
 		LOG_ERROR("couldn't write to FT2232 to disable loopback");
-		return ERROR_JTAG_INIT_FAILED;
+		return ft2232_quit(), ERROR_JTAG_INIT_FAILED;
 	}
 
 #if BUILD_FT2232_FTD2XX == 1
-	return ft2232_purge_ftd2xx();
+	retval = ft2232_purge_ftd2xx();
 #elif BUILD_FT2232_LIBFTDI == 1
-	return ft2232_purge_libftdi();
+	retval = ft2232_purge_libftdi();
 #endif
+	if (retval != ERROR_OK)
+		return ft2232_quit(), ERROR_JTAG_INIT_FAILED;
 
 	return ERROR_OK;
 }
@@ -3220,24 +3243,6 @@ static void flossjtag_blink(void)
 	buffer_write(high_direction);
 }
 
-static int ft2232_quit(void)
-{
-#if BUILD_FT2232_FTD2XX == 1
-	FT_STATUS status;
-
-	status = FT_Close(ftdih);
-#elif BUILD_FT2232_LIBFTDI == 1
-	ftdi_usb_close(&ftdic);
-
-	ftdi_deinit(&ftdic);
-#endif
-
-	free(ft2232_buffer);
-	ft2232_buffer = NULL;
-
-	return ERROR_OK;
-}
-
 COMMAND_HANDLER(ft2232_handle_device_desc_command)
 {
 	char *cp;
-- 
1.7.0.4

_______________________________________________
Openocd-development mailing list
Openocd-development@lists.berlios.de
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to