Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>

#include "config.h"

Expand Down Expand Up @@ -65,6 +66,27 @@
// Return a+b, unless a is NOPTS. b must not be NOPTS.
#define MP_ADD_PTS(a, b) ((a) == MP_NOPTS_VALUE ? (a) : ((a) + (b)))

// Return the maximum value representable by integer type of a.
#define MP_MAX_VAL(a) _Generic((a), \
char: SCHAR_MAX, \
unsigned char: UCHAR_MAX, \
short: SHRT_MAX, \
unsigned short: USHRT_MAX, \
int: INT_MAX, \
unsigned int: UINT_MAX, \
long: LONG_MAX, \
unsigned long: ULONG_MAX, \
long long: LLONG_MAX, \
unsigned long long: ULLONG_MAX)

// Multiply a * b and store in result. If result would overflow, saturate at
// its maximum value instead, and return true. Otherwise, return false.
#define MP_SATURATE_MUL(result, a, b) ( \
MP_CKD_MUL(result, a, b) ? \
*result = MP_MAX_VAL(*result), true : \
false \
)

#define CONTROL_OK 1
#define CONTROL_TRUE 1
#define CONTROL_FALSE 0
Expand Down
2 changes: 1 addition & 1 deletion demux/demux_edl.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ static struct tl_root *parse_edl(bstr str, struct mp_log *log)
: get_meta(tl, index);
sh->lang = get_param0(&ctx, sh, "lang");
sh->title = get_param0(&ctx, sh, "title");
sh->hls_bitrate = get_param_int(&ctx, "byterate", 0) * 8;
MP_SATURATE_MUL(&sh->hls_bitrate, get_param_int(&ctx, "byterate", 0), 8);
int pid = get_param_int(&ctx, "program_id", -1);
if (pid >= 0)
MP_TARRAY_APPEND(sh, sh->program_ids, sh->num_program_ids, pid);
Expand Down
7 changes: 7 additions & 0 deletions osdep/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define MP_EXPAND_ARGS(...) __VA_ARGS__

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
#include <stdckdint.h>
#define MP_NORETURN [[noreturn]]
#define MP_FALLTHROUGH [[fallthrough]]
#define MP_WARN_UNUSED_RESULT [[nodiscard]]
Expand Down Expand Up @@ -78,4 +79,10 @@
#define MP_SCANF_ATTRIBUTE(a1, a2)
#endif

#if defined(__STDC_VERSION_STDCKDINT_H__) && __STDC_VERSION_STDCKDINT_H__ >= 202311L
#define MP_CKD_MUL(result, a, b) ckd_mul(result, a, b)
#elif __has_builtin(__builtin_mul_overflow)
#define MP_CKD_MUL(result, a, b) __builtin_mul_overflow(a, b, result)
#endif

#endif
Loading