Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore debug output indentation in catch clause #2134

Merged
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions libyara/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ static void exception_handler(int sig, siginfo_t * info, void *context)
}
}

// Keep debug output indentation level consistent
#if 0 == YR_DEBUG_VERBOSITY
#define YR_DEBUG_INDENT_RESET_INIT
#define YR_DEBUG_INDENT_RESET_RESTORE
#else
extern YR_TLS int yr_debug_indent;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

Consider adding a comment explaining the purpose of this variable, specifically how it's used for debug indentation. For example, you could say something like "Thread-local variable to track the indentation level for debug output."

Suggested change
extern YR_TLS int yr_debug_indent;
extern YR_TLS int yr_debug_indent; // Thread-local variable to track the indentation level for debug output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable is declared elsewhere. No need to comment on it here.

// Ugly, but unfortunately cannot use ifdef macros inside a macro
#define YR_DEBUG_INDENT_RESET_INIT int yr_debug_indent_before_jump = yr_debug_indent;
#define YR_DEBUG_INDENT_RESET_RESTORE yr_debug_indent = yr_debug_indent_before_jump;
#endif

typedef struct sigaction sa;

#define YR_TRYCATCH(_do_, _try_clause_, _catch_clause_) \
Expand All @@ -243,6 +254,8 @@ typedef struct sigaction sa;
} \
exception_handler_usecount++; \
pthread_mutex_unlock(&exception_handler_mutex); \
/* Save the current debug indentation level before the jump. */ \
YR_DEBUG_INDENT_RESET_INIT \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using ...

#if 0 == YR_DEBUG_VERBOSITY
int yr_debug_indent_before_jump = yr_debug_indent;
#endif 

Instead of declaring yet another macro. It's easier to follow the code this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct me if I'm mistaken but I think you cannot do that within YR_TRYCATCH where both macros are used. YR_TRYCATCH is a function-like macro thus, effectively, a single line (also indicated by the trailing backslashes). There, you cannot insert a construct like #if ... #endif which requires the # directives to be the first non-whitespace characters on each line.

However, I pushed a slightly different approach which uses yet another function-like macro to set yr_debug_indent in the catch clause. It might be somewhat nicer to read, but please decide for yourself. One drawback, though: we get a compiler warning (per YR_TRYCATCH instance) about an unused variable (which is only cosmetic; the compiler should optimize it away).

jumpinfo ji; \
ji.memfault_from = 0; \
ji.memfault_to = 0; \
Expand All @@ -256,6 +269,9 @@ typedef struct sigaction sa;
} \
else \
{ \
/* Restore debug output indentation in case of failure */ \
YR_DEBUG_INDENT_RESET_RESTORE \
plusvic marked this conversation as resolved.
Show resolved Hide resolved
\
_catch_clause_ \
} \
pthread_mutex_lock(&exception_handler_mutex); \
Expand Down
Loading