Skip to content

Commit

Permalink
Avoid out of bound access to emb_exception->stacktrace
Browse files Browse the repository at this point in the history
  • Loading branch information
priettt committed Dec 28, 2024
1 parent 958cc32 commit e537269
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion embrace-android-sdk/src/main/cpp/serializer/file_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ bool emb_add_exc_info_to_json(const emb_crash *crash, JSON_Object *crash_object,
bool emb_add_exc_to_json(const emb_exception *exception, JSON_Array *frames_object) {
EMB_LOGDEV("About to serialize %d stack frames.", (int) exception->num_sframes);

for (int i = 0; i < exception->num_sframes; ++i) {
for (int i = 0; i < exception->num_sframes && i < kEMBMaxSFrames; ++i) {
JSON_Value *frame_value = json_value_init_object();
if (frame_value == NULL) {
return false;
Expand Down
3 changes: 3 additions & 0 deletions embrace-android-sdk/src/main/cpp/unwinders/stack_unwinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ static inline void emb_copy_frame_data(unwindstack::AndroidUnwinderData &android
int k = 0;

for (const auto &frame: android_unwinder_data.frames) {
if (k >= kEMBMaxSFrames) {
break;
}
emb_sframe *data = &stacktrace[k++];

// populate the link register for the first value only
Expand Down
8 changes: 4 additions & 4 deletions embrace-android-sdk/src/main/cpp/unwinders/unwinder.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
void emb_fix_fileinfo(ssize_t frame_count,
emb_sframe stacktrace[kEMBMaxSFrames]) {
static Dl_info info;
for (int i = 0; i < frame_count; ++i) {

for (int i = 0; i < frame_count && i < kEMBMaxSFrames; ++i) {
if (dladdr((void *)stacktrace[i].frame_addr, &info) != 0) {
stacktrace[i].module_addr = (uintptr_t)info.dli_fbase;
stacktrace[i].offset_addr = (uintptr_t)info.dli_saddr;
stacktrace[i].line_num =
stacktrace[i].frame_addr - stacktrace[i].module_addr;
stacktrace[i].line_num = stacktrace[i].frame_addr - stacktrace[i].module_addr;
if (info.dli_fname != NULL) {
emb_strncpy(stacktrace[i].filename, (char *)info.dli_fname, sizeof(stacktrace[i].filename));
}
Expand All @@ -36,4 +36,4 @@ ssize_t emb_process_capture(emb_env *env, siginfo_t *info, void *user_context) {
emb_fix_fileinfo(frame_count, env->crash.capture.stacktrace);

return frame_count;
}
}

0 comments on commit e537269

Please sign in to comment.