Skip to content

In release mode, Investigate counter update to fail in compile time if the counter being updated is not defined. #262

Description

@yamingk

Right now updating a counter that is not defined will cause memory corruption in release mode.

Slack Discuss: https://ebay-eng.slack.com/archives/C07JWRZGG4D/p1740761894559139

Some Discussion History:
From Yaming Kuang:
See code below, for debug build, if someone is updating counter that doesn't exist, it should be able to throw runtime assert error, line: 263.
For release build, it won't, because see line:275, there is no check, it is just pretending it got valid index (this index could be anything) and then try to increase it.

#define COUNTER_INCREMENT(group, name, ...)                                                                            \
    __VALIDATE_AND_EXECUTE(group, NamedCounter, counter_increment, name, __VA_ARGS__)


258 #ifndef NDEBUG
259 #define __VALIDATE_AND_EXECUTE(group, type, method, name, ...)                                                         \
260     {                                                                                                                  \
261         using namespace sisl;                                                                                          \
262         const auto index{METRIC_NAME_TO_INDEX(type, name)};                                                            \
263         if (index == std::numeric_limits< decltype(index) >::max()) {                                                  \
264             fprintf(stderr, "Metric name '%s' not registered yet but used\n", BOOST_PP_STRINGIZE(name));               \
265             fflush(stderr);                                                                                            \
266             assert(0);                                                                                                 \
267         }                                                                                                              \
268         ((group).m_impl_ptr->method(index, __VA_ARGS__));                                                              \
269     }
270 #else
271 #define __VALIDATE_AND_EXECUTE(group, type, method, name, ...)                                                         \
272     {                                                                                                                  \
273         using namespace sisl;                                                                                          \
274         const auto index{METRIC_NAME_TO_INDEX(type, name)};                                                            \
275         ((group).m_impl_ptr->method(index, __VA_ARGS__));                                                              \
276     }
277 #endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions