Skip to content

Commit

Permalink
gdb_packet: Check vasprintf return value harder and skip free().
Browse files Browse the repository at this point in the history
* Initialize *buf on stack.
* If vasprintf fails to allocate, *buf is old value, not a pointer on heap;
  freeing it crashes firmware.
* Emit a constant string to logging channel for builds with diagnostics.
  • Loading branch information
ALTracer committed May 27, 2024
1 parent 8b4d376 commit e4da644
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/gdb_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,17 @@ void gdb_put_notification(const char *const packet, const size_t size)
void gdb_putpacket_f(const char *const fmt, ...)
{
va_list ap;
char *buf;
char *buf = NULL;

va_start(ap, fmt);
const int size = vasprintf(&buf, fmt, ap);
if (size > 0)
if (size < 0) {
/* Heap exhaustion. Report with puts() elsewhere. */
DEBUG_ERROR("gdb_putpacket_f: vasprintf failed\n");
} else {
gdb_putpacket(buf, size);
free(buf);
free(buf);
}
va_end(ap);
}

Expand Down

0 comments on commit e4da644

Please sign in to comment.