Skip to content

Commit

Permalink
Use COUNT_OF macro for message_handlers bounds check
Browse files Browse the repository at this point in the history
Summary: Currently we calculate the size of a 2d array with messy sizeof calculations. In this diff, we pull in a macro from chromium (sourced from SO) to make this calculation simpler and semantically more clear.

Reviewed By: vikg-fb

Differential Revision: D14690805

fbshipit-source-id: c106198a8ddc902e3cfc7fe85364e462eb716e30
  • Loading branch information
sciencemanx authored and facebook-github-bot committed Apr 3, 2019
1 parent 5bdee78 commit 861a7b3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lte/gateway/c/oai/common/common_defs.h
Expand Up @@ -43,6 +43,9 @@
#define CLONE_REF

#define OFFSET_OF(TyPe, MeMBeR) ((size_t) & ((TyPe *) 0)->MeMBeR)
// https://stackoverflow.com/questions/4415524/common-array-length-macro-for-c
#define COUNT_OF(x) \
((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x])))))

#define PARENT_STRUCT(cOnTaiNeD, TyPe, MeMBeR) \
({ \
Expand Down
5 changes: 2 additions & 3 deletions lte/gateway/c/oai/tasks/s1ap/s1ap_mme_handlers.c
Expand Up @@ -195,9 +195,8 @@ int s1ap_mme_handle_message(
* Checking procedure Code and direction of message
*/
if (
(message->procedureCode >
(sizeof(message_handlers) / (3 * sizeof(s1ap_message_handler_t)))) ||
(message->direction > S1AP_PDU_PR_unsuccessfulOutcome)) {
message->procedureCode >= COUNT_OF(message_handlers) ||
message->direction > S1AP_PDU_PR_unsuccessfulOutcome) {
OAILOG_DEBUG(
LOG_S1AP,
"[SCTP %d] Either procedureCode %d or direction %d exceed expected\n",
Expand Down

0 comments on commit 861a7b3

Please sign in to comment.