iemejia commented on code in PR #3863:
URL: https://github.com/apache/avro/pull/3863#discussion_r3567562257
##########
lang/php/lib/Datum/AvroIODatumReader.php:
##########
@@ -538,4 +614,119 @@ private function readDecimal(string $bytes, int $scale):
string
return (string) ($scale > 0 ? ($int / (10 ** $scale)) : $int);
}
+
+ /**
+ * Minimum number of bytes a single value of the given schema can occupy on
+ * the wire. Used to reject an array/map block count that could not be
backed
+ * by the bytes remaining. It returns 0 for any schema that can encode to
+ * zero bytes: the null primitive, but also a record with no fields or
whose
+ * fields all encode to zero bytes. A zero return disables the collection
+ * check for that element type (so, e.g., an array of nulls is not falsely
+ * rejected). Types that cannot be resolved cheaply default to 1.
+ *
+ * @param array<int, bool> $visited
+ */
+ private static function minBytesPerElement(mixed $schema, array $visited =
[]): int
+ {
+ $type = $schema instanceof AvroSchema ? $schema->type() : $schema;
+ // Named/complex field types may nest a schema object; unwrap one
level.
+ if ($type instanceof AvroSchema) {
+ return self::minBytesPerElement($type, $visited);
+ }
+ if (!is_string($type)) {
+ return 1;
+ }
+ switch ($type) {
+ case AvroSchema::NULL_TYPE:
+ return 0;
+ case AvroSchema::FLOAT_TYPE:
+ return 4;
+ case AvroSchema::DOUBLE_TYPE:
+ return 8;
+ case AvroSchema::FIXED_SCHEMA:
+ return $schema instanceof AvroFixedSchema ? $schema->size() :
1;
Review Comment:
Fixed — minBytesPerElement now returns `max(0, $schema->size())` for fixed,
so a malformed negative fixed size can't make the minimum negative and cause
the element to be mis-treated as zero-byte.
--
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]