mbutrovich commented on code in PR #3354:
URL: https://github.com/apache/iceberg-rust/pull/3354#discussion_r4198161957


##########
crates/iceberg/src/avro/named_types.rs:
##########
@@ -0,0 +1,255 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Reading Avro files whose schema defines a named type more than once.
+//!
+//! The Avro specification allows one definition per full name. iceberg-rust
+//! wrote manifests that define a `fixed` or `decimal` type once for every 
field
+//! that uses it, while `apache-avro` 0.21 accepted that. `apache-avro` 0.22
+//! rejects such a file's header. Rewriting each repeated definition as a
+//! reference to the first one keeps those files readable, because a reference
+//! decodes the same bytes as the definition it refers to.
+
+use std::collections::HashMap;
+use std::io::Cursor;
+
+use apache_avro::Schema as AvroSchema;
+use apache_avro::reader::datum::GenericDatumReader;
+use apache_avro::schema::MapSchema;
+use apache_avro::types::Value as AvroValue;
+use apache_avro::writer::datum::GenericDatumWriter;
+use serde_json::{Map, Value as JsonValue};
+
+use crate::Result;
+use crate::error::invalid_data;
+
+const MAGIC: &[u8] = b"Obj\x01";
+const SCHEMA_KEY: &str = "avro.schema";
+
+/// Rewrites the header of an Avro object container file so that each named 
type
+/// is defined once and referenced by name afterwards. Returns the rewritten
+/// file and the full names of the repeated types, or `None` if a repeated
+/// definition differs from the first one, which leaves the file ambiguous.
+pub(crate) fn define_named_types_once(bs: &[u8]) -> Result<Option<(Vec<u8>, 
Vec<String>)>> {
+    let body = bs
+        .strip_prefix(MAGIC)
+        .ok_or_else(|| invalid_data!("Not an Avro object container file"))?;
+    // The header's metadata is an Avro `map<bytes>`, followed by the sync 
marker.
+    let metadata_schema = AvroSchema::Map(MapSchema {
+        types: Box::new(AvroSchema::Bytes),
+        attributes: Default::default(),
+    });
+    let mut cursor = Cursor::new(body);
+    let AvroValue::Map(mut metadata) = 
GenericDatumReader::builder(&metadata_schema)
+        .build()?
+        .read_value(&mut cursor)?
+    else {
+        return Err(invalid_data!("Avro file metadata is not a map"));
+    };
+    let rest = &body[cursor.position() as usize..];

Review Comment:
   > `cursor.position() as usize` then slicing is a latent panic on untrusted 
header bytes.
   
   
[Fixed](https://github.com/apache/iceberg-rust/blob/91486882b29deb0d97560543e0764aa00a2988fd/crates/iceberg/src/avro/named_types.rs#L73-L76).
 It converts with `usize::try_from` and slices with `get`, and returns a 
`DataInvalid` error if the position is out of range.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to