Copilot commented on code in PR #17:
URL: https://github.com/apache/sedona-db/pull/17#discussion_r2330580099


##########
c/sedona-s2geography/build.rs:
##########
@@ -119,3 +144,71 @@ fn parse_cmake_linker_flags(binary_dir: &Path) {
         }
     }
 }
+
+fn find_cmake_linker_flags(binary_dir: &Path) -> PathBuf {
+    // Usually lib but could be lib64 (e.g., the Linux used for wheel builds)
+    let possible_lib_dirs = ["lib", "lib64", "build/Release"];
+    for possible_lib in possible_lib_dirs {
+        let path = binary_dir.join(possible_lib).join("linker_flags.txt");
+        if path.exists() {
+            return path;
+        }
+    }
+
+    panic!(
+        "Can't find linker_flags.txt output at {}",
+        binary_dir.to_string_lossy()
+    )
+}
+
+fn find_lib_dir(binary_dir: &Path, lib_file: &str) -> PathBuf {
+    // Usually lib but could be lib64 (e.g., the Linux used for wheel builds)
+    let possible_lib_dirs = ["lib", "lib64", "build/Release"];
+    for possible_lib in possible_lib_dirs {
+        let path = binary_dir.join(possible_lib);
+        let static_lib_posix = path.join(format!("lib{lib_file}.a"));
+        let static_lib_windows = path.join(format!("{lib_file}.lib"));
+        if static_lib_posix.exists() || static_lib_windows.exists() {
+            return path;
+        }
+    }
+
+    panic!(
+        "Can't find library dir for static library '{lib_file}' output at {}",
+        binary_dir.to_string_lossy()
+    )
+}
+
+// Linker flags scraped from MSBuild are UTF-16 with a byte order mark; linker 
flags scraped otherwise
+// are system encoding (likely UTF-8 or compatible).
+fn read_file_maybe_utf16(path: &PathBuf) -> String {
+    let linker_flags_bytes = std::fs::read(path).expect("Read 
linker_flags.txt");
+
+    // Check if the first two bytes are UTF-16 BOM (0xFF 0xFE or 0xFE 0xFF)
+    if linker_flags_bytes.len() >= 2
+        && ((linker_flags_bytes[0] == 0xFF && linker_flags_bytes[1] == 0xFE)
+            || (linker_flags_bytes[0] == 0xFE && linker_flags_bytes[1] == 
0xFF))
+    {
+        // Determine endianness from BOM
+        let is_le = linker_flags_bytes[0] == 0xFF;
+
+        // Skip the BOM and convert the rest
+        let u16_bytes = &linker_flags_bytes[2..];
+        let u16_slice = unsafe {
+            std::slice::from_raw_parts(u16_bytes.as_ptr() as *const u16, 
u16_bytes.len() / 2)
+        };
+
+        if is_le {
+            String::from_utf16_lossy(u16_slice).to_string()
+        } else {
+            // Convert from big endian to host endian
+            let mut swapped = Vec::with_capacity(u16_slice.len());
+            for &value in u16_slice {
+                swapped.push(u16::from_be(value));
+            }
+            String::from_utf16_lossy(&swapped).to_string()
+        }

Review Comment:
   The unsafe block creates a slice from raw bytes without proper alignment 
validation. This could cause undefined behavior if the byte array is not 
properly aligned for u16. Consider using a safer method like `bytemuck` crate 
or adding alignment checks before the unsafe operation.
   ```suggestion
           let u16_vec: Vec<u16> = u16_bytes
               .chunks_exact(2)
               .map(|chunk| {
                   if is_le {
                       u16::from_le_bytes([chunk[0], chunk[1]])
                   } else {
                       u16::from_be_bytes([chunk[0], chunk[1]])
                   }
               })
               .collect();
   
           String::from_utf16_lossy(&u16_vec).to_string()
   ```



-- 
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