Skip to content

Commit

Permalink
Use heap memory instead of stack for large variables
Browse files Browse the repository at this point in the history
The default stack size for musl-libc is 128kb. Therefore we should try to keep stack
allocations well below this limit in order to avoid stack overflows.
  • Loading branch information
JFreegman committed Nov 21, 2020
1 parent 5d2b1e3 commit 00f2f41
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16d62d5c5aa261d6efefbe8e94cc25a68c76ea923ae92ce56b0d22b32326ae0b /usr/local/bin/tox-bootstrapd
ca03a05f92fb3169c588ff0894c7a1d4466d71c36e3a7ddbe804797f15d6c126 /usr/local/bin/tox-bootstrapd
13 changes: 11 additions & 2 deletions toxav/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,15 @@ void ac_iterate(ACSession *ac)
return;
}

/* TODO(mannol): fix this and jitter buffering */
/* TODO: fix this and jitter buffering */

/* Enough space for the maximum frame size (120 ms 48 KHz stereo audio) */
int16_t temp_audio_buffer[AUDIO_MAX_BUFFER_SIZE_PCM16 * AUDIO_MAX_CHANNEL_COUNT];
int16_t *temp_audio_buffer = (int16_t *)malloc(AUDIO_MAX_BUFFER_SIZE_PCM16 * AUDIO_MAX_CHANNEL_COUNT * sizeof(int16_t));

if (temp_audio_buffer == nullptr) {
LOGGER_ERROR(ac->log, "Failed to allocate memory for audio buffer");
return;
}

pthread_mutex_lock(ac->queue_mutex);
struct JitterBuffer *const j_buf = (struct JitterBuffer *)ac->j_buf;
Expand Down Expand Up @@ -195,10 +200,14 @@ void ac_iterate(ACSession *ac)
ac->lp_sampling_rate, ac->acb_user_data);
}

free(temp_audio_buffer);

return;
}

pthread_mutex_unlock(ac->queue_mutex);

free(temp_audio_buffer);
}

int ac_queue_message(Mono_Time *mono_time, void *acp, struct RTPMessage *msg)
Expand Down
9 changes: 8 additions & 1 deletion toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -2830,7 +2830,12 @@ void dht_save(const DHT *dht, uint8_t *data)
/* get right offset. we write the actual header later. */
data = state_write_section_header(data, DHT_STATE_COOKIE_TYPE, 0, 0);

Node_format clients[MAX_SAVED_DHT_NODES];
Node_format *clients = (Node_format *)malloc(MAX_SAVED_DHT_NODES * sizeof(Node_format));

if (clients == nullptr) {
LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES);
return;
}

uint32_t num = 0;

Expand Down Expand Up @@ -2873,6 +2878,8 @@ void dht_save(const DHT *dht, uint8_t *data)

state_write_section_header(old_data, DHT_STATE_COOKIE_TYPE, pack_nodes(data, sizeof(Node_format) * num, clients, num),
DHT_STATE_TYPE_NODES);

free(clients);
}

/* Bootstrap from this number of nodes every time dht_connect_after_load() is called */
Expand Down

0 comments on commit 00f2f41

Please sign in to comment.