Check for memory allocation errors

Appending data to the buffer may fail if a memory allocation is
necessary to enlarge the buffer. Hence the return value of the
dc_buffer_append() call should always be checked, unless the memory was
already pre-allocated or the check is deferred after the last operation.
This commit is contained in:
Jef Driesen 2018-01-11 22:07:56 +01:00
parent c194f559d8
commit 0ae9e355f8
5 changed files with 44 additions and 7 deletions

View File

@ -185,7 +185,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, "Insufficient buffer space available.");
return status;
}
// Send the command.
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);

View File

@ -493,7 +493,12 @@ divesystem_idive_device_foreach (dc_device_t *abstract, dc_dive_callback_t callb
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_append(buffer, packet, commands->header.size)) {
ERROR (abstract->context, "Insufficient buffer space available.");
dc_buffer_free(buffer);
return rc;
}
for (unsigned int j = 0; j < nsamples; j += commands->nsamples) {
unsigned int idx = j + 1;
@ -518,7 +523,11 @@ 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.");
dc_buffer_free(buffer);
return rc;
}
}
unsigned char *data = dc_buffer_get_data(buffer);

View File

@ -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) {

View File

@ -1150,7 +1150,11 @@ 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.");
fclose (fp);
return DC_STATUS_NOMEMORY;
}
}
// Close the file.

View File

@ -94,6 +94,15 @@ static const dc_device_vtable_t suunto_eonsteel_device_vtable = {
static const char dive_directory[] = "0:/dives";
static void file_list_free (struct directory_entry *de)
{
while (de) {
struct directory_entry *next = de->next;
free (de);
de = next;
}
}
static struct directory_entry *alloc_dirent(int type, int len, const char *name)
{
struct directory_entry *res;
@ -414,7 +423,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;
}
@ -674,7 +686,13 @@ suunto_eonsteel_device_foreach(dc_device_t *abstract, dc_dive_callback_t callbac
de = de->next;
}
file = dc_buffer_new(0);
file = dc_buffer_new (16384);
if (file == NULL) {
ERROR (abstract->context, "Insufficient buffer space available.");
file_list_free (latest);
return DC_STATUS_NOMEMORY;
}
progress.maximum = count;
progress.current = 0;
device_event_emit(abstract, DC_EVENT_PROGRESS, &progress);