Skip to content

Commit

Permalink
display_gl_get_property: assert correct size
Browse files Browse the repository at this point in the history
Enough space is rather assumed because in the oppiste case, hard-to-detect
behavior may occur, especially while the problem is not reported.
  • Loading branch information
MartinPulec committed Jan 22, 2024
1 parent a39408d commit 45f51f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
10 changes: 9 additions & 1 deletion src/utils/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@
#endif // defined EXTERN_C

/// unconditional alternative to assert that is not affected by NDEBUG macro
#define UG_ASSERT(cond) do { if (!(cond)) { fprintf(stderr, "%s:%d: %s: Assertion `" #cond "' failed.\n", __FILE__, __LINE__, __func__); abort(); } } while(0)
#define UG_ASSERT(cond) \
do { /* NOLINT(cppcoreguidelines-avoid-do-while) */ \
if (!(cond)) { \
fprintf(stderr, \
"%s:%d: %s: Assertion `" #cond "' failed.\n", \
__FILE__, __LINE__, __func__); \
abort(); \
} \
} while (0)

/// shortcut for `snprintf(var, sizeof var...)`, `var` must be a char array
#define snprintf_ch(str, ...) snprintf(str, sizeof str, __VA_ARGS__)
Expand Down
17 changes: 5 additions & 12 deletions src/video_display/gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1897,9 +1897,7 @@ display_gl_get_property(void *state, int property, void *val, size_t *len)

switch (property) {
case DISPLAY_PROPERTY_CODECS: {
if (sizeof gl_supp_codecs > *len) {
return false;
}
UG_ASSERT(*len >= sizeof gl_supp_codecs);
auto filter_codecs = [s](codec_t c) {
if (get_bits_per_component(c) > DEPTH8 &&
commandline_params.find(
Expand All @@ -1925,23 +1923,18 @@ display_gl_get_property(void *state, int property, void *val, size_t *len)
return true;
}
case DISPLAY_PROPERTY_RGB_SHIFT:
if (sizeof(rgb_shift) > *len) {
return false;
}
UG_ASSERT(*len >= sizeof rgb_shift);
memcpy(val, rgb_shift, sizeof(rgb_shift));
*len = sizeof(rgb_shift);
return true;
case DISPLAY_PROPERTY_BUF_PITCH:
UG_ASSERT(*len >= sizeof(int));
*(int *) val = PITCH_DEFAULT;
*len = sizeof(int);
return true;
case DISPLAY_PROPERTY_SUPPORTED_IL_MODES:
if (sizeof(supported_il_modes) <= *len) {
memcpy(val, supported_il_modes,
sizeof(supported_il_modes));
} else {
return false;
}
UG_ASSERT(*len >= sizeof supported_il_modes);
memcpy(val, supported_il_modes, sizeof(supported_il_modes));
*len = sizeof(supported_il_modes);
return true;
default:
Expand Down

0 comments on commit 45f51f8

Please sign in to comment.