mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2026-04-29 06:46:38 -04:00
spa: add and use spa_overflow macros
This commit is contained in:
parent
84f8230a47
commit
0f8d5c6e57
16 changed files with 149 additions and 50 deletions
|
|
@ -28,6 +28,7 @@
|
|||
#include <spa/support/plugin.h>
|
||||
#include <spa/utils/json.h>
|
||||
#include <spa/utils/names.h>
|
||||
#include <spa/utils/overflow.h>
|
||||
#include <spa/utils/result.h>
|
||||
#include <spa/utils/ringbuffer.h>
|
||||
#include <spa/utils/string.h>
|
||||
|
|
@ -1179,9 +1180,16 @@ static int setup_streams(struct impl *impl)
|
|||
|
||||
spa_pod_dynamic_builder_clean(&b);
|
||||
|
||||
impl->rec_ringsize = (size_t)sizeof(float) * impl->max_buffer_size * impl->rec_info.rate / 1000;
|
||||
impl->play_ringsize = (size_t)sizeof(float) * ((size_t)impl->max_buffer_size * impl->play_info.rate / 1000 + impl->buffer_delay);
|
||||
impl->out_ringsize = (size_t)sizeof(float) * impl->max_buffer_size * impl->out_info.rate / 1000;
|
||||
if (spa_overflow_mul(impl->max_buffer_size, impl->rec_info.rate / 1000, &impl->rec_ringsize) ||
|
||||
spa_overflow_mul(impl->rec_ringsize, (uint32_t)sizeof(float), &impl->rec_ringsize))
|
||||
return -ENOMEM;
|
||||
if (spa_overflow_mul(impl->max_buffer_size, impl->play_info.rate / 1000, &impl->play_ringsize) ||
|
||||
spa_overflow_add(impl->play_ringsize, impl->buffer_delay, &impl->play_ringsize) ||
|
||||
spa_overflow_mul(impl->play_ringsize, (uint32_t)sizeof(float), &impl->play_ringsize))
|
||||
return -ENOMEM;
|
||||
if (spa_overflow_mul(impl->max_buffer_size, impl->out_info.rate / 1000, &impl->out_ringsize) ||
|
||||
spa_overflow_mul(impl->out_ringsize, (uint32_t)sizeof(float), &impl->out_ringsize))
|
||||
return -ENOMEM;
|
||||
for (i = 0; i < impl->rec_info.channels; i++) {
|
||||
impl->rec_buffer[i] = malloc(impl->rec_ringsize);
|
||||
if (impl->rec_buffer[i] == NULL)
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
#include <spa/param/latency-utils.h>
|
||||
#include <spa/param/tag-utils.h>
|
||||
#include <spa/utils/overflow.h>
|
||||
#include <spa/param/audio/raw-json.h>
|
||||
#include <spa/pod/dynamic.h>
|
||||
#include <spa/filter-graph/filter-graph.h>
|
||||
|
|
@ -1764,7 +1765,13 @@ static int setup_streams(struct impl *impl)
|
|||
res = -ENOMEM;
|
||||
goto done;
|
||||
}
|
||||
if ((params = calloc(n_params+1, sizeof(struct spa_pod*))) == NULL) {
|
||||
size_t params_alloc;
|
||||
if (spa_overflow_add((size_t)n_params, (size_t)1, ¶ms_alloc) ||
|
||||
spa_overflow_mul(params_alloc, sizeof(struct spa_pod*), ¶ms_alloc)) {
|
||||
res = -ENOMEM;
|
||||
goto done;
|
||||
}
|
||||
if ((params = calloc(1, params_alloc)) == NULL) {
|
||||
res = -errno;
|
||||
goto done;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
#include <spa/utils/string.h>
|
||||
#include <spa/utils/json.h>
|
||||
#include <spa/utils/ringbuffer.h>
|
||||
#include <spa/utils/overflow.h>
|
||||
#include <spa/param/latency-utils.h>
|
||||
#include <spa/param/audio/raw-json.h>
|
||||
#include <spa/debug/types.h>
|
||||
|
|
@ -578,14 +579,14 @@ static void recalculate_buffer(struct impl *impl)
|
|||
void *data;
|
||||
size_t alloc_size;
|
||||
|
||||
if (delay > (UINT32_MAX / 4) - (1u<<15)) {
|
||||
if (spa_overflow_add(delay, 1u << 15, &impl->buffer_size) ||
|
||||
spa_overflow_mul(impl->buffer_size, 4u, &impl->buffer_size)) {
|
||||
pw_log_warn("delay too large, delay disabled");
|
||||
impl->buffer_size = 0;
|
||||
free(impl->buffer_data);
|
||||
impl->buffer_data = NULL;
|
||||
goto done;
|
||||
}
|
||||
impl->buffer_size = (delay + (1u<<15)) * 4;
|
||||
alloc_size = (size_t)impl->buffer_size * impl->channels;
|
||||
data = realloc(impl->buffer_data, alloc_size);
|
||||
if (data == NULL) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
#include <spa/utils/endian.h>
|
||||
#include <spa/utils/overflow.h>
|
||||
#include <spa/control/ump-utils.h>
|
||||
|
||||
#ifdef HAVE_OPUS_CUSTOM
|
||||
|
|
@ -142,11 +143,11 @@ static int netjack2_init(struct netjack2_peer *peer)
|
|||
|
||||
max_midi_ch = SPA_MAX(peer->params.send_midi_channels, peer->params.recv_midi_channels);
|
||||
if (max_midi_ch > MAX_CHANNELS ||
|
||||
peer->params.period_size > UINT32_MAX / sizeof(float) / SPA_MAX(max_midi_ch, 1u)) {
|
||||
spa_overflow_mul(peer->params.period_size, (uint32_t)sizeof(float), &peer->midi_size) ||
|
||||
spa_overflow_mul(peer->midi_size, max_midi_ch, &peer->midi_size)) {
|
||||
errno = EINVAL;
|
||||
goto error_errno;
|
||||
}
|
||||
peer->midi_size = peer->params.period_size * sizeof(float) * max_midi_ch;
|
||||
if ((peer->midi_data = calloc(1, peer->midi_size)) == NULL && peer->midi_size > 0)
|
||||
goto error_errno;
|
||||
|
||||
|
|
@ -157,13 +158,11 @@ static int netjack2_init(struct netjack2_peer *peer)
|
|||
}
|
||||
|
||||
if (peer->params.sample_encoder == NJ2_ENCODER_INT) {
|
||||
peer->max_encoded_size = peer->params.period_size * sizeof(int16_t);
|
||||
if (peer->params.period_size > UINT32_MAX / sizeof(int16_t) ||
|
||||
(max_audio_ch > 0 && peer->max_encoded_size > UINT32_MAX / max_audio_ch)) {
|
||||
if (spa_overflow_mul(peer->params.period_size, (uint32_t)sizeof(int16_t), &peer->max_encoded_size) ||
|
||||
spa_overflow_mul(peer->max_encoded_size, max_audio_ch, &peer->encoded_size)) {
|
||||
errno = EINVAL;
|
||||
goto error_errno;
|
||||
}
|
||||
peer->encoded_size = peer->max_encoded_size * max_audio_ch;
|
||||
if ((peer->encoded_data = calloc(1, peer->encoded_size)) == NULL)
|
||||
goto error_errno;
|
||||
} else if (peer->params.sample_encoder == NJ2_ENCODER_OPUS) {
|
||||
|
|
@ -175,11 +174,10 @@ static int netjack2_init(struct netjack2_peer *peer)
|
|||
}
|
||||
peer->max_encoded_size = ((uint64_t)peer->params.kbps * peer->params.period_size * 1024) /
|
||||
(peer->params.sample_rate * 8) + sizeof(uint16_t);
|
||||
if (max_audio_ch > 0 && peer->max_encoded_size > UINT32_MAX / max_audio_ch) {
|
||||
if (spa_overflow_mul(peer->max_encoded_size, max_audio_ch, &peer->encoded_size)) {
|
||||
errno = EINVAL;
|
||||
goto error_errno;
|
||||
}
|
||||
peer->encoded_size = peer->max_encoded_size * max_audio_ch;
|
||||
if ((peer->encoded_data = calloc(1, peer->encoded_size)) == NULL)
|
||||
goto error_errno;
|
||||
if ((peer->opus_config = opus_custom_mode_create(peer->params.sample_rate,
|
||||
|
|
@ -800,9 +798,8 @@ static int netjack2_recv_midi(struct netjack2_peer *peer, struct nj2_packet_head
|
|||
peer->sync.num_packets = ntohl(header->num_packets);
|
||||
max_size = peer->params.mtu - sizeof(*header);
|
||||
|
||||
if (sub_cycle > 0 && max_size > UINT32_MAX / sub_cycle)
|
||||
if (spa_overflow_mul(max_size, sub_cycle, &offset))
|
||||
return -EOVERFLOW;
|
||||
offset = max_size * sub_cycle;
|
||||
|
||||
data += sizeof(*header);
|
||||
len -= sizeof(*header);
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
#include <netdb.h>
|
||||
|
||||
#include <spa/utils/result.h>
|
||||
#include <spa/utils/overflow.h>
|
||||
#include <spa/debug/mem.h>
|
||||
|
||||
#include "config.h"
|
||||
|
|
@ -644,9 +645,8 @@ static int handle_input(struct pw_websocket_connection *conn)
|
|||
current)) < 0)
|
||||
return res;
|
||||
|
||||
if (conn->data_wanted > SIZE_MAX - res)
|
||||
if (spa_overflow_add(conn->data_wanted, (size_t)res, &conn->data_wanted))
|
||||
return -EOVERFLOW;
|
||||
conn->data_wanted += res;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
|
@ -1020,14 +1020,14 @@ int pw_websocket_connection_send(struct pw_websocket_connection *conn, uint8_t o
|
|||
size_t payload_length = 0;
|
||||
|
||||
for (i = 0; i < iov_len; i++) {
|
||||
if (payload_length > SIZE_MAX - iov[i].iov_len)
|
||||
if (spa_overflow_add(payload_length, iov[i].iov_len, &payload_length))
|
||||
return -EOVERFLOW;
|
||||
payload_length += iov[i].iov_len;
|
||||
}
|
||||
if (payload_length > SIZE_MAX - sizeof(*msg) - 14)
|
||||
size_t alloc_size;
|
||||
if (spa_overflow_add(payload_length, sizeof(*msg) + 14, &alloc_size))
|
||||
return -EOVERFLOW;
|
||||
|
||||
if ((msg = calloc(1, sizeof(*msg) + 14 + payload_length)) == NULL)
|
||||
if ((msg = calloc(1, alloc_size)) == NULL)
|
||||
return -errno;
|
||||
|
||||
d = msg->data;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue