Skip to content

Commit

Permalink
added safety comments for linux.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ashu26jha committed Jul 26, 2024
1 parent d73f432 commit 489c6e6
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions utils/env_preferences/src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ pub mod linux_prefs {
pub fn get_locales() -> HashMap<LocaleCategory, String> {
let mut locale_map = HashMap::new();

// SAFETY: Safety is ensured because we pass a `NULL` pointer and retrieve the locale there is
// no subsequent calls for `setlocale` which could change the locale of this particular thread
let locales_ptr = unsafe { setlocale(LC_ALL, ptr::null()) };

if locales_ptr.is_null() {
locale_map.insert(LocaleCategory::All, "C".to_string());
return locale_map;
}

// SAFETY: A valid `NULL` terminator is present which is a requirement of `from_ptr`
let locales_cstr = unsafe { CStr::from_ptr(locales_ptr) };

if let Ok(locales_str) = locales_cstr.to_str() {
Expand All @@ -83,9 +86,12 @@ pub mod linux_prefs {
}

pub fn get_system_calendars() -> impl Iterator<Item = (Cow<'static, str>, Cow<'static, str>)> {
// SAFETY: Safety is ensured because we pass a `NULL` pointer and retrieve the locale there is
// no subsequent calls for `setlocale` which could change the locale of this particular thread
let locale_ptr = unsafe { setlocale(LC_TIME, ptr::null()) };

if !locale_ptr.is_null() {
// SAFETY: A valid `NULL` terminator is present which is a requirement of `from_ptr`
let c_str = unsafe { CStr::from_ptr(locale_ptr) };

if let Ok(str_slice) = c_str.to_str() {
Expand Down

0 comments on commit 489c6e6

Please sign in to comment.