This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch 
avro-3758-use-atomic-types-instead-of-static-mut
in repository https://gitbox.apache.org/repos/asf/avro.git

commit ceb31b0b39a6b4f8f2602286fdd7328e2bbe5a5e
Author: Martin Tzvetanov Grigorov <[email protected]>
AuthorDate: Thu May 11 10:15:49 2023 +0300

    AVRO-3758: [Rust] Use AtomicXyz types instead of static mutable ones
    
    Use AtomicUsize for apache_avro::util::MAX_ALLOCATION_BYTES
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
---
 lang/rust/avro/src/de.rs   |  2 +-
 lang/rust/avro/src/ser.rs  |  2 +-
 lang/rust/avro/src/util.rs | 23 ++++++++++++++---------
 3 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/lang/rust/avro/src/de.rs b/lang/rust/avro/src/de.rs
index c20aa5f4b..a1b52e6c5 100644
--- a/lang/rust/avro/src/de.rs
+++ b/lang/rust/avro/src/de.rs
@@ -649,9 +649,9 @@ pub fn from_value<'de, D: Deserialize<'de>>(value: &'de 
Value) -> Result<D, Erro
 
 #[cfg(test)]
 mod tests {
-    use std::sync::atomic::Ordering;
     use pretty_assertions::assert_eq;
     use serde::Serialize;
+    use std::sync::atomic::Ordering;
     use uuid::Uuid;
 
     use super::*;
diff --git a/lang/rust/avro/src/ser.rs b/lang/rust/avro/src/ser.rs
index a5203e3be..2237c1d99 100644
--- a/lang/rust/avro/src/ser.rs
+++ b/lang/rust/avro/src/ser.rs
@@ -488,10 +488,10 @@ pub fn to_value<S: Serialize>(value: S) -> Result<Value, 
Error> {
 
 #[cfg(test)]
 mod tests {
-    use std::sync::atomic::Ordering;
     use super::*;
     use pretty_assertions::assert_eq;
     use serde::{Deserialize, Serialize};
+    use std::sync::atomic::Ordering;
 
     #[derive(Debug, Deserialize, Serialize, Clone)]
     struct Test {
diff --git a/lang/rust/avro/src/util.rs b/lang/rust/avro/src/util.rs
index 4dc115947..d94acce7a 100644
--- a/lang/rust/avro/src/util.rs
+++ b/lang/rust/avro/src/util.rs
@@ -17,15 +17,22 @@
 
 use crate::{schema::Documentation, AvroResult, Error};
 use serde_json::{Map, Value};
-use std::{convert::TryFrom, i64, io::Read, sync::Once};
-use std::sync::atomic::{AtomicBool, Ordering};
+use std::{
+    convert::TryFrom,
+    i64,
+    io::Read,
+    sync::{
+        atomic::{AtomicBool, AtomicUsize, Ordering},
+        Once,
+    },
+};
 
 /// Maximum number of bytes that can be allocated when decoding
 /// Avro-encoded values. This is a protection against ill-formed
 /// data, whose length field might be interpreted as enormous.
 /// See max_allocation_bytes to change this limit.
 pub const DEFAULT_MAX_ALLOCATION_BYTES: usize = 512 * 1024 * 1024;
-static mut MAX_ALLOCATION_BYTES: usize = DEFAULT_MAX_ALLOCATION_BYTES;
+static MAX_ALLOCATION_BYTES: AtomicUsize = 
AtomicUsize::new(DEFAULT_MAX_ALLOCATION_BYTES);
 static MAX_ALLOCATION_BYTES_ONCE: Once = Once::new();
 
 /// Whether to set serialization & deserialization traits
@@ -140,12 +147,10 @@ fn decode_variable<R: Read>(reader: &mut R) -> 
AvroResult<u64> {
 /// to set the limit either when calling this method, or when decoding for
 /// the first time.
 pub fn max_allocation_bytes(num_bytes: usize) -> usize {
-    unsafe {
-        MAX_ALLOCATION_BYTES_ONCE.call_once(|| {
-            MAX_ALLOCATION_BYTES = num_bytes;
-        });
-        MAX_ALLOCATION_BYTES
-    }
+    MAX_ALLOCATION_BYTES_ONCE.call_once(|| {
+        MAX_ALLOCATION_BYTES.store(num_bytes, Ordering::Release);
+    });
+    MAX_ALLOCATION_BYTES.load(Ordering::Acquire)
 }
 
 pub fn safe_len(len: usize) -> AvroResult<usize> {

Reply via email to