Copilot commented on code in PR #603: URL: https://github.com/apache/iceberg-cpp/pull/603#discussion_r2979497079
########## src/iceberg/puffin/puffin_json_internal.cc: ########## @@ -0,0 +1,127 @@ +/* + * 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. + */ + +#include "iceberg/puffin/puffin_json_internal.h" + +#include <nlohmann/json.hpp> + +#include "iceberg/util/json_util_internal.h" +#include "iceberg/util/macros.h" + +namespace iceberg::puffin { + +namespace { +constexpr std::string_view kBlobs = "blobs"; +constexpr std::string_view kProperties = "properties"; +constexpr std::string_view kType = "type"; +constexpr std::string_view kFields = "fields"; +constexpr std::string_view kSnapshotId = "snapshot-id"; +constexpr std::string_view kSequenceNumber = "sequence-number"; +constexpr std::string_view kOffset = "offset"; +constexpr std::string_view kLength = "length"; +constexpr std::string_view kCompressionCodec = "compression-codec"; +} // namespace + +nlohmann::json ToJson(const BlobMetadata& blob_metadata) { + nlohmann::json json; + json[kType] = blob_metadata.type; + json[kFields] = blob_metadata.input_fields; + json[kSnapshotId] = blob_metadata.snapshot_id; + json[kSequenceNumber] = blob_metadata.sequence_number; + json[kOffset] = blob_metadata.offset; + json[kLength] = blob_metadata.length; + + SetOptionalStringField(json, kCompressionCodec, blob_metadata.compression_codec); + SetContainerField(json, kProperties, blob_metadata.properties); + + return json; +} + +Result<BlobMetadata> BlobMetadataFromJson(const nlohmann::json& json) { + BlobMetadata blob_metadata; + + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.type, GetJsonValue<std::string>(json, kType)); + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.input_fields, + GetJsonValue<std::vector<int32_t>>(json, kFields)); + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.snapshot_id, + GetJsonValue<int64_t>(json, kSnapshotId)); + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.sequence_number, + GetJsonValue<int64_t>(json, kSequenceNumber)); + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.offset, GetJsonValue<int64_t>(json, kOffset)); + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.length, GetJsonValue<int64_t>(json, kLength)); + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.compression_codec, + GetJsonValueOrDefault<std::string>(json, kCompressionCodec)); + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.properties, + FromJsonMap<std::string>(json, kProperties)); + + return blob_metadata; +} + +nlohmann::json ToJson(const FileMetadata& file_metadata) { + nlohmann::json json; + + nlohmann::json blobs_json = nlohmann::json::array(); + for (const auto& blob : file_metadata.blobs) { + blobs_json.push_back(ToJson(blob)); + } + json[kBlobs] = std::move(blobs_json); + + SetContainerField(json, kProperties, file_metadata.properties); + + return json; +} + +Result<FileMetadata> FileMetadataFromJson(const nlohmann::json& json) { + FileMetadata file_metadata; + + ICEBERG_ASSIGN_OR_RAISE(auto blobs_json, GetJsonValue<nlohmann::json>(json, kBlobs)); + if (!blobs_json.is_array()) { + return InvalidArgument("Cannot parse blobs from non-array: {}", + SafeDumpJson(blobs_json)); Review Comment: `FileMetadataFromJson` returns `InvalidArgument` when the `blobs` field is present but not an array. For JSON deserialization errors elsewhere in the codebase, `JsonParseError` is used (e.g., `json_serde.cc` and `json_util_internal.h`), which makes error kinds consistent for callers that route parse errors differently from argument validation. Consider returning `JsonParseError` here as well. ```suggestion return JsonParseError("Cannot parse blobs from non-array: {}", SafeDumpJson(blobs_json)); ``` ########## src/iceberg/puffin/meson.build: ########## @@ -15,4 +15,4 @@ # specific language governing permissions and limitations # under the License. -install_headers(['file_metadata.h'], subdir: 'iceberg/puffin') +install_headers(['file_metadata.h', 'puffin_format.h', 'puffin_json_internal.h'], subdir: 'iceberg/puffin') Review Comment: `puffin_json_internal.h` is being installed as a public header even though it is explicitly marked "internal" (and other `*_internal.h` headers in this repo are not installed). Installing it expands the supported public API surface and also exposes a dependency on `nlohmann::json` in the public headers. Consider either (a) removing it from `install_headers` and keeping it internal-only, or (b) renaming/promoting it to a stable public header (and include `nlohmann/json.hpp` instead of only the fwd header if it remains in the API). ```suggestion install_headers(['file_metadata.h', 'puffin_format.h'], subdir: 'iceberg/puffin') ``` ########## src/iceberg/puffin/puffin_format.h: ########## @@ -0,0 +1,89 @@ +/* + * 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. + */ + +#pragma once + +/// \file iceberg/puffin/puffin_format.h +/// Puffin file format constants and utilities. + +#include <array> +#include <cstdint> +#include <span> +#include <vector> + +#include "iceberg/iceberg_export.h" +#include "iceberg/puffin/file_metadata.h" +#include "iceberg/result.h" + +namespace iceberg::puffin { + +/// \brief Puffin file format constants. +struct ICEBERG_EXPORT PuffinFormat { + /// Magic bytes: "PFA1" (Puffin Fratercula arctica, version 1) + static constexpr std::array<uint8_t, 4> kMagic = {0x50, 0x46, 0x41, 0x31}; + + static constexpr int32_t kMagicLength = 4; + static constexpr int32_t kFooterStartMagicOffset = 0; + static constexpr int32_t kFooterStartMagicLength = kMagicLength; + static constexpr int32_t kFooterStructPayloadSizeOffset = 0; + static constexpr int32_t kFooterStructFlagsOffset = kFooterStructPayloadSizeOffset + 4; + static constexpr int32_t kFooterStructFlagsLength = 4; + static constexpr int32_t kFooterStructMagicOffset = + kFooterStructFlagsOffset + kFooterStructFlagsLength; + + /// Total length of the footer struct: payload_size(4) + flags(4) + magic(4) + static constexpr int32_t kFooterStructLength = kFooterStructMagicOffset + kMagicLength; + + /// Default compression codec for footer payload. + static constexpr PuffinCompressionCodec kFooterCompressionCodec = + PuffinCompressionCodec::kLz4; Review Comment: `PuffinFormat::kFooterCompressionCodec` is set to `kLz4`, but `Compress/Decompress` currently return `NotSupported` for LZ4. This constant is likely to be used as the default when writing/reading footer payloads, and will cause immediate failures once that code lands. Consider defaulting to `kNone` until LZ4 support is implemented, or clearly documenting that the default codec is not yet supported and must not be used for writing. ```suggestion PuffinCompressionCodec::kNone; ``` ########## src/iceberg/puffin/puffin_format.cc: ########## @@ -0,0 +1,92 @@ +/* + * 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. + */ + +#include "iceberg/puffin/puffin_format.h" + +#include <cstring> +#include <utility> + +#include "iceberg/util/endian.h" + +namespace iceberg::puffin { + +namespace { + +constexpr std::pair<int, int> GetFlagPosition(PuffinFlag flag) { + switch (flag) { + case PuffinFlag::kFooterPayloadCompressed: + return {0, 0}; + } + std::unreachable(); +} + +} // namespace + +bool IsFlagSet(std::span<const uint8_t, 4> flags, PuffinFlag flag) { + auto [byte_num, bit_num] = GetFlagPosition(flag); + return (flags[byte_num] & (1 << bit_num)) != 0; +} + +void SetFlag(std::span<uint8_t, 4> flags, PuffinFlag flag) { + auto [byte_num, bit_num] = GetFlagPosition(flag); + flags[byte_num] |= (1 << bit_num); +} + +void WriteInt32LittleEndian(int32_t value, std::span<uint8_t, 4> output) { + auto le_value = ToLittleEndian(value); + std::memcpy(output.data(), &le_value, sizeof(le_value)); +} + +int32_t ReadInt32LittleEndian(std::span<const uint8_t, 4> input) { + int32_t value; + std::memcpy(&value, input.data(), sizeof(value)); + return FromLittleEndian(value); +} + +int32_t ReadInt32LittleEndian(std::span<const uint8_t> data, int32_t offset) { Review Comment: `ReadInt32LittleEndian(std::span<const uint8_t> data, int32_t offset)` constructs a fixed-size span from `data.data() + offset` without validating `offset` (negative values or `offset + 4 > data.size()`), which can lead to out-of-bounds reads/UB when parsing untrusted buffers. Since this API is newly introduced, consider changing it to return `Result<int32_t>` (or removing the overload) and performing bounds checks (or at least an `ICEBERG_DCHECK`) before forming the subspan. ```suggestion int32_t ReadInt32LittleEndian(std::span<const uint8_t> data, int32_t offset) { ICEBERG_DCHECK(offset >= 0); ICEBERG_DCHECK(static_cast<size_t>(offset) + 4 <= data.size()); ``` -- 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]
