[PATCH 11/14] Cleanup: check error return values of buffer handling

Dirk Hohndel dirk at hohndel.org
Wed Jan 3 11:35:14 PST 2018


This is a farily big change and in some cases these checks are redundant
as we reserved the necessary space already. But from a consistency
perspective it makes more sense to always check the return value.

Coverity CID 207798

Signed-off-by: Dirk Hohndel <dirk at hohndel.org>
---
 src/atomics_cobalt.c    |  6 +++++-
 src/citizen_aqualand.c  |  5 ++++-
 src/diverite_nitekq.c   | 10 ++++++++--
 src/divesystem_idive.c  | 15 ++++++++++++---
 src/hw_ostc.c           |  5 ++++-
 src/hw_ostc3.c          |  5 ++++-
 src/mares_nemo.c        | 15 ++++++++++++---
 src/oceanic_vtpro.c     |  5 ++++-
 src/reefnet_sensus.c    |  5 ++++-
 src/reefnet_sensuspro.c |  5 ++++-
 src/suunto_eon.c        |  5 ++++-
 src/suunto_eonsteel.c   | 10 ++++++++--
 src/suunto_vyper.c      |  5 ++++-
 src/uwatec_aladin.c     |  5 ++++-
 src/uwatec_memomouse.c  |  5 ++++-
 15 files changed, 85 insertions(+), 21 deletions(-)

diff --git a/src/atomics_cobalt.c b/src/atomics_cobalt.c
index a5ce98c9a302..5335eba94e47 100644
--- a/src/atomics_cobalt.c
+++ b/src/atomics_cobalt.c
@@ -297,7 +297,11 @@ atomics_cobalt_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init,
 		}
 
 		// Append the packet to the output buffer.
-		dc_buffer_append (buffer, packet, length);
+		if (!dc_buffer_append (buffer, packet, length)) {
+			ERROR (abstract->context, "Insufficient buffer space available.");
+			return DC_STATUS_NOMEMORY;
+		}
+
 		nbytes += length;
 
 		// If we received fewer bytes than requested, the transfer is finished.
diff --git a/src/citizen_aqualand.c b/src/citizen_aqualand.c
index cfcdbb85ee81..f639efdd9566 100644
--- a/src/citizen_aqualand.c
+++ b/src/citizen_aqualand.c
@@ -184,7 +184,10 @@ citizen_aqualand_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
 			return status;
 		}
 
-		dc_buffer_append(buffer, answer, sizeof (answer));
+		if (!dc_buffer_append(buffer, answer, sizeof (answer))) {
+			ERROR (abstract->context, "Failed to allocate memory.");
+			return DC_STATUS_NOMEMORY;
+		}
 
 		// Send the command.
 		status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
diff --git a/src/diverite_nitekq.c b/src/diverite_nitekq.c
index 9ed1fa7df538..11628f7e4066 100644
--- a/src/diverite_nitekq.c
+++ b/src/diverite_nitekq.c
@@ -298,7 +298,10 @@ diverite_nitekq_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
 		return rc;
 	}
 
-	dc_buffer_append (buffer, packet, sizeof (packet));
+	if (!dc_buffer_append (buffer, packet, sizeof (packet))) {
+		ERROR (abstract->context, "Insufficient buffer space available.");
+		return DC_STATUS_NOMEMORY;
+	}
 
 	// Update and emit a progress event.
 	progress.current += SZ_PACKET;
@@ -323,7 +326,10 @@ diverite_nitekq_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
 			return rc;
 		}
 
-		dc_buffer_append (buffer, packet, sizeof (packet));
+		if (!dc_buffer_append (buffer, packet, sizeof (packet))) {
+			ERROR (abstract->context, "Insufficient buffer space available.");
+			return DC_STATUS_NOMEMORY;
+		}
 
 		// Update and emit a progress event.
 		progress.current += SZ_PACKET;
diff --git a/src/divesystem_idive.c b/src/divesystem_idive.c
index 642aaee4a05e..6fcdfabafc5c 100644
--- a/src/divesystem_idive.c
+++ b/src/divesystem_idive.c
@@ -491,8 +491,14 @@ divesystem_idive_device_foreach (dc_device_t *abstract, dc_dive_callback_t callb
 		device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
 
 		dc_buffer_clear(buffer);
-		dc_buffer_reserve(buffer, commands->header.size + commands->sample.size * nsamples);
-		dc_buffer_append(buffer, packet, commands->header.size);
+		if (!dc_buffer_reserve(buffer, commands->header.size + commands->sample.size * nsamples)) {
+			ERROR (abstract->context, "Insufficient buffer space available.");
+			return DC_STATUS_NOMEMORY;
+		}
+		if (!dc_buffer_append(buffer, packet, commands->header.size)) {
+			ERROR (abstract->context, "Insufficient buffer space available.");
+			return DC_STATUS_NOMEMORY;
+		}
 
 		for (unsigned int j = 0; j < nsamples; j += commands->nsamples) {
 			unsigned int idx = j + 1;
@@ -517,7 +523,10 @@ divesystem_idive_device_foreach (dc_device_t *abstract, dc_dive_callback_t callb
 			progress.current = i * NSTEPS + STEP(j + n + 1, nsamples + 1);
 			device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
 
-			dc_buffer_append(buffer, packet, commands->sample.size * n);
+			if (!dc_buffer_append(buffer, packet, commands->sample.size * n)) {
+				ERROR (abstract->context, "Insufficient buffer space available.");
+				return DC_STATUS_NOMEMORY;
+			}
 		}
 
 		unsigned char *data = dc_buffer_get_data(buffer);
diff --git a/src/hw_ostc.c b/src/hw_ostc.c
index 4e4335caf291..ed81fdf293df 100644
--- a/src/hw_ostc.c
+++ b/src/hw_ostc.c
@@ -587,7 +587,10 @@ hw_ostc_device_screenshot (dc_device_t *abstract, dc_buffer_t *buffer, hw_ostc_f
 
 		if (format == HW_OSTC_FORMAT_RAW) {
 			// Append the raw data to the output buffer.
-			dc_buffer_append (buffer, raw, nbytes);
+			if (!dc_buffer_append (buffer, raw, nbytes)) {
+				ERROR (abstract->context, "Insufficient buffer space available.");
+				return DC_STATUS_NOMEMORY;
+			}
 		} else {
 			// Store the decompressed data in the output buffer.
 			for (unsigned int i = 0; i < count; ++i) {
diff --git a/src/hw_ostc3.c b/src/hw_ostc3.c
index eca8dfbc30e8..15862c255443 100644
--- a/src/hw_ostc3.c
+++ b/src/hw_ostc3.c
@@ -1150,7 +1150,10 @@ hw_ostc3_firmware_readfile4 (dc_buffer_t *buffer, dc_context_t *context, const c
 	size_t n = 0;
 	unsigned char block[1024] = {0};
 	while ((n = fread (block, 1, sizeof (block), fp)) > 0) {
-		dc_buffer_append (buffer, block, n);
+		if (dc_buffer_append (buffer, block, n)) {
+			ERROR (context, "Insufficient buffer space available.");
+			return DC_STATUS_NOMEMORY;
+		}
 	}
 
 	// Close the file.
diff --git a/src/mares_nemo.c b/src/mares_nemo.c
index b3d262a5c18c..fc8a9a389d36 100644
--- a/src/mares_nemo.c
+++ b/src/mares_nemo.c
@@ -256,15 +256,24 @@ mares_nemo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
 				ERROR (abstract->context, "Both packets are not equal.");
 				return DC_STATUS_PROTOCOL;
 			}
-			dc_buffer_append (buffer, packet, PACKETSIZE);
+			if (!dc_buffer_append (buffer, packet, PACKETSIZE)) {
+				ERROR (abstract->context, "Insufficient buffer space available.");
+				return DC_STATUS_NOMEMORY;
+			}
 		} else if (crc1 == ccrc1) {
 			// Only the first packet has a correct checksum.
 			WARNING (abstract->context, "Only the first packet has a correct checksum.");
-			dc_buffer_append (buffer, packet, PACKETSIZE);
+			if (!dc_buffer_append (buffer, packet, PACKETSIZE)) {
+				ERROR (abstract->context, "Insufficient buffer space available.");
+				return DC_STATUS_NOMEMORY;
+			}
 		} else if (crc2 == ccrc2) {
 			// Only the second packet has a correct checksum.
 			WARNING (abstract->context, "Only the second packet has a correct checksum.");
-			dc_buffer_append (buffer, packet + PACKETSIZE + 1, PACKETSIZE);
+			if (!dc_buffer_append (buffer, packet + PACKETSIZE + 1, PACKETSIZE)) {
+				ERROR (abstract->context, "Insufficient buffer space available.");
+				return DC_STATUS_NOMEMORY;
+			}
 		} else {
 			ERROR (abstract->context, "Unexpected answer checksum.");
 			return DC_STATUS_PROTOCOL;
diff --git a/src/oceanic_vtpro.c b/src/oceanic_vtpro.c
index ca3a2fc2a479..cdb8943930b7 100644
--- a/src/oceanic_vtpro.c
+++ b/src/oceanic_vtpro.c
@@ -364,7 +364,10 @@ oceanic_aeris500ai_device_logbook (dc_device_t *abstract, dc_event_progress_t *p
 		if (memcmp (answer, device->base.fingerprint, PAGESIZE / 2) == 0) {
 			dc_buffer_clear (logbook);
 		} else {
-			dc_buffer_append (logbook, answer, PAGESIZE / 2);
+			if (!dc_buffer_append (logbook, answer, PAGESIZE / 2)) {
+				ERROR (abstract->context, "Insufficient buffer space available.");
+				return DC_STATUS_NOMEMORY;
+			}
 		}
 	}
 
diff --git a/src/reefnet_sensus.c b/src/reefnet_sensus.c
index 29b9bb1c8429..b6462309cb0d 100644
--- a/src/reefnet_sensus.c
+++ b/src/reefnet_sensus.c
@@ -344,7 +344,10 @@ reefnet_sensus_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
 		return DC_STATUS_PROTOCOL;
 	}
 
-	dc_buffer_append (buffer, answer + 4, SZ_MEMORY);
+	if (!dc_buffer_append (buffer, answer + 4, SZ_MEMORY)) {
+		ERROR (abstract->context, "Insufficient buffer space available.");
+		return DC_STATUS_NOMEMORY;
+	}
 
 	return DC_STATUS_SUCCESS;
 }
diff --git a/src/reefnet_sensuspro.c b/src/reefnet_sensuspro.c
index b6bdd6bfe751..88c165028f12 100644
--- a/src/reefnet_sensuspro.c
+++ b/src/reefnet_sensuspro.c
@@ -308,7 +308,10 @@ reefnet_sensuspro_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
 		return DC_STATUS_PROTOCOL;
 	}
 
-	dc_buffer_append (buffer, answer, SZ_MEMORY);
+	if (!dc_buffer_append (buffer, answer, SZ_MEMORY)) {
+		ERROR (abstract->context, "Insuffiecient buffer space.");
+		return DC_STATUS_NOMEMORY;
+	}
 
 	return DC_STATUS_SUCCESS;
 }
diff --git a/src/suunto_eon.c b/src/suunto_eon.c
index 1fbcaa11c74b..de5685558b53 100644
--- a/src/suunto_eon.c
+++ b/src/suunto_eon.c
@@ -209,7 +209,10 @@ suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
 		return DC_STATUS_PROTOCOL;
 	}
 
-	dc_buffer_append (buffer, answer, SZ_MEMORY);
+	if (!dc_buffer_append (buffer, answer, SZ_MEMORY)) {
+		ERROR (abstract->context, "Insufficient buffer space available.");
+		return DC_STATUS_NOMEMORY;
+	}
 
 	return DC_STATUS_SUCCESS;
 }
diff --git a/src/suunto_eonsteel.c b/src/suunto_eonsteel.c
index cbf1b756d945..948d1d329076 100644
--- a/src/suunto_eonsteel.c
+++ b/src/suunto_eonsteel.c
@@ -593,7 +593,10 @@ static int read_file(suunto_eonsteel_device_t *eon, const char *filename, dc_buf
 
 		if (got > size)
 			got = size;
-		dc_buffer_append(buf, result+8, got);
+		if (!dc_buffer_append(buf, result+8, got)) {
+			ERROR(eon->base.context, "Insufficient buffer space available.");
+			return -1;
+		}
 		offset += got;
 		size -= got;
 	}
@@ -863,7 +866,10 @@ suunto_eonsteel_device_foreach(dc_device_t *abstract, dc_dive_callback_t callbac
 			// Reset the membuffer, put the 4-byte length at the head.
 			dc_buffer_clear(file);
 			put_le32(time, buf);
-			dc_buffer_append(file, buf, 4);
+			if (!dc_buffer_append(file, buf, 4)) {
+				ERROR(abstract->context, "Insufficient buffer space available.");
+				return DC_STATUS_NOMEMORY;
+			}
 
 			// Then read the filename into the rest of the buffer
 			rc = read_file(eon, pathname, file);
diff --git a/src/suunto_vyper.c b/src/suunto_vyper.c
index cc4680bd7dbe..586a01716dfd 100644
--- a/src/suunto_vyper.c
+++ b/src/suunto_vyper.c
@@ -410,7 +410,10 @@ suunto_vyper_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init, dc
 		// transfer is finished. This approach leaves no data behind in
 		// the serial receive buffer, and if this packet is part of the
 		// last incomplete dive, no error has to be reported at all.
-		dc_buffer_append (buffer, answer + 2, len);
+		if (!dc_buffer_append (buffer, answer + 2, len)) {
+			ERROR (abstract->context, "Insufficient buffer space available.");
+			return DC_STATUS_NOMEMORY;
+		}
 
 		nbytes += len;
 
diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c
index 8fcd918bfcf8..d6993db4ad8d 100644
--- a/src/uwatec_aladin.c
+++ b/src/uwatec_aladin.c
@@ -250,7 +250,10 @@ uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
 	clock.devtime = device->devtime;
 	device_event_emit (abstract, DC_EVENT_CLOCK, &clock);
 
-	dc_buffer_append (buffer, answer, SZ_MEMORY);
+	if (!dc_buffer_append (buffer, answer, SZ_MEMORY)) {
+		ERROR (abstract->context, "Insufficient buffer space available.");
+		return DC_STATUS_NOMEMORY;
+	}
 
 	return DC_STATUS_SUCCESS;
 }
diff --git a/src/uwatec_memomouse.c b/src/uwatec_memomouse.c
index e1bd94978a68..e55b13dffb56 100644
--- a/src/uwatec_memomouse.c
+++ b/src/uwatec_memomouse.c
@@ -306,7 +306,10 @@ uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, dc_buffer
 		}
 
 		// Append the packet to the buffer.
-		dc_buffer_append (buffer, packet + 1, length);
+		if (!dc_buffer_append (buffer, packet + 1, length)) {
+			ERROR (abstract->context, "Insufficient buffer space available.");
+			return DC_STATUS_NOMEMORY;
+		}
 
 		nbytes += length;
 	}
-- 
2.15.1



More information about the devel mailing list