Skip to content

Commit 45f51f8

Browse files
committed
display_gl_get_property: assert correct size
Enough space is rather assumed because in the oppiste case, hard-to-detect behavior may occur, especially while the problem is not reported.
1 parent a39408d commit 45f51f8

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/utils/macros.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,15 @@
7373
#endif // defined EXTERN_C
7474

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

7886
/// shortcut for `snprintf(var, sizeof var...)`, `var` must be a char array
7987
#define snprintf_ch(str, ...) snprintf(str, sizeof str, __VA_ARGS__)

src/video_display/gl.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,9 +1897,7 @@ display_gl_get_property(void *state, int property, void *val, size_t *len)
18971897

18981898
switch (property) {
18991899
case DISPLAY_PROPERTY_CODECS: {
1900-
if (sizeof gl_supp_codecs > *len) {
1901-
return false;
1902-
}
1900+
UG_ASSERT(*len >= sizeof gl_supp_codecs);
19031901
auto filter_codecs = [s](codec_t c) {
19041902
if (get_bits_per_component(c) > DEPTH8 &&
19051903
commandline_params.find(
@@ -1925,23 +1923,18 @@ display_gl_get_property(void *state, int property, void *val, size_t *len)
19251923
return true;
19261924
}
19271925
case DISPLAY_PROPERTY_RGB_SHIFT:
1928-
if (sizeof(rgb_shift) > *len) {
1929-
return false;
1930-
}
1926+
UG_ASSERT(*len >= sizeof rgb_shift);
19311927
memcpy(val, rgb_shift, sizeof(rgb_shift));
19321928
*len = sizeof(rgb_shift);
19331929
return true;
19341930
case DISPLAY_PROPERTY_BUF_PITCH:
1931+
UG_ASSERT(*len >= sizeof(int));
19351932
*(int *) val = PITCH_DEFAULT;
19361933
*len = sizeof(int);
19371934
return true;
19381935
case DISPLAY_PROPERTY_SUPPORTED_IL_MODES:
1939-
if (sizeof(supported_il_modes) <= *len) {
1940-
memcpy(val, supported_il_modes,
1941-
sizeof(supported_il_modes));
1942-
} else {
1943-
return false;
1944-
}
1936+
UG_ASSERT(*len >= sizeof supported_il_modes);
1937+
memcpy(val, supported_il_modes, sizeof(supported_il_modes));
19451938
*len = sizeof(supported_il_modes);
19461939
return true;
19471940
default:

0 commit comments

Comments
 (0)