@@ -100,7 +100,12 @@ bool inject_on_main(int pid, const char *lib_path) {
100100 For arm32 compatibility, we set the last bit to the same as the entry address
101101 */
102102
103- uintptr_t break_addr = (-0x05ec1cff & ~1 ) | ((uintptr_t )entry_addr & 1 );
103+ /* INFO: (-0x0F & ~1) is a value below zero, while the one after "|"
104+ is an unsigned (must be 0 or greater) value, so we must
105+ cast the second value to signed long (intptr_t) to avoid
106+ undefined behavior.
107+ */
108+ uintptr_t break_addr = (uintptr_t )((intptr_t )(-0x0F & ~1 ) | (intptr_t )((uintptr_t )entry_addr & 1 ));
104109 if (!write_proc (pid, (uintptr_t )addr_of_entry_addr, &break_addr, sizeof (break_addr))) return false ;
105110
106111 ptrace (PTRACE_CONT, pid, 0 , 0 );
@@ -110,7 +115,7 @@ bool inject_on_main(int pid, const char *lib_path) {
110115 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGSEGV) {
111116 if (!get_regs (pid, regs)) return false ;
112117
113- if (static_cast < uintptr_t >( regs.REG_IP & ~1 ) != (break_addr & ~1 )) {
118+ if ((( int ) regs.REG_IP & ~1 ) != (( int ) break_addr & ~1 )) {
114119 LOGE (" stopped at unknown addr %p" , (void *) regs.REG_IP );
115120
116121 return false ;
@@ -184,8 +189,14 @@ bool inject_on_main(int pid, const char *lib_path) {
184189 }
185190
186191 /* NOTICE: C++ -> C */
187- char *err = (char *)malloc (dlerror_len + 1 );
188- read_proc (pid, (uintptr_t ) dlerror_str_addr, err, dlerror_len);
192+ char *err = (char *)malloc ((dlerror_len + 1 ) * sizeof (char ));
193+ if (err == NULL ) {
194+ LOGE (" malloc err" );
195+
196+ return false ;
197+ }
198+
199+ read_proc (pid, dlerror_str_addr, err, dlerror_len + 1 );
189200
190201 LOGE (" dlerror info %s" , err);
191202
0 commit comments