lriggs commented on code in PR #49471:
URL: https://github.com/apache/arrow/pull/49471#discussion_r2915196794


##########
cpp/src/gandiva/gdv_string_function_stubs.cc:
##########
@@ -407,14 +407,17 @@ const char* gdv_fn_substring_index(int64_t context, const 
char* txt, int32_t txt
     }
   }
 
-  if (static_cast<int32_t>(abs(cnt)) <= static_cast<int32_t>(occ.size()) && 
cnt > 0) {
+  // Use int64_t to avoid undefined behavior with abs(INT_MIN)
+  int64_t abs_cnt = (cnt < 0) ? -static_cast<int64_t>(cnt) : 
static_cast<int64_t>(cnt);
+  int64_t occ_size = static_cast<int64_t>(occ.size());
+
+  if (abs_cnt <= occ_size && cnt > 0) {
     memcpy(out, txt, occ[cnt - 1]);
     *out_len = occ[cnt - 1];
     return out;
-  } else if (static_cast<int32_t>(abs(cnt)) <= 
static_cast<int32_t>(occ.size()) &&
-             cnt < 0) {
+  } else if (abs_cnt <= occ_size && cnt < 0) {
     int32_t sz = static_cast<int32_t>(occ.size());
-    int32_t temp = static_cast<int32_t>(abs(cnt));
+    int32_t temp = static_cast<int32_t>(abs_cnt);

Review Comment:
   Since this is dealing with edge cases I think you could use int64_t here 
instead and avoid the potential of an overflow in the cast.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to