Skip to content

Commit a756248

Browse files
committed
fix: add bounds checking for inputsize/outputsize casts in coreml proto
Validate InputSize and OutputSize are non-negative before casting to ulong to prevent negative values from wrapping to large unsigned values in CoreML protobuf serialization.
1 parent 344913d commit a756248

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/Deployment/Mobile/CoreML/CoreMLProto.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ private static byte[] CreateInnerProductLayer(CoreMLLayer layer)
263263
using var stream = new MemoryStream();
264264
using var writer = new CodedOutputStream(stream);
265265

266+
// Validate layer sizes before casting to prevent negative values from wrapping to large unsigned values
267+
if (layer.InputSize < 0 || layer.OutputSize < 0)
268+
throw new ArgumentException(
269+
$"Layer '{layer.Name}' has invalid size: InputSize={layer.InputSize}, OutputSize={layer.OutputSize}. " +
270+
"Both must be non-negative for CoreML protobuf serialization.");
271+
266272
// Field 1: inputChannels
267273
writer.WriteTag(1, WireFormat.WireType.Varint);
268274
writer.WriteUInt64((ulong)layer.InputSize);

0 commit comments

Comments
 (0)