flashrom.c: Validate before allocate in verify_range()

Simplify a goto away for free'ing a buffer by validating
before attempting to allocate.

Change-Id: Iae886f203d1c59ae9a89421f7483a4ec3f747256
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/59372
Original-Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Original-Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/71439
Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/flashrom.c b/flashrom.c
index 2872a11..126b9a1 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -400,6 +400,13 @@
 	if (!len)
 		return -1;
 
+	if (start + len > flash->chip->total_size * 1024) {
+		msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
+			" total_size 0x%x\n", __func__, start, len,
+			flash->chip->total_size * 1024);
+		return -1;
+	}
+
 	if (!flash->chip->read) {
 		msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
 		return -1;
@@ -410,17 +417,8 @@
 		msg_gerr("Could not allocate memory!\n");
 		return -1;
 	}
-	int ret = 0;
 
-	if (start + len > flash->chip->total_size * 1024) {
-		msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
-			" total_size 0x%x\n", __func__, start, len,
-			flash->chip->total_size * 1024);
-		ret = -1;
-		goto out_free;
-	}
-
-	ret = flash->chip->read(flash, readbuf, start, len);
+	int ret = flash->chip->read(flash, readbuf, start, len);
 	if (ret) {
 		msg_gerr("Verification impossible because read failed "
 			 "at 0x%x (len 0x%x)\n", start, len);