fix: handle missing content-length header in statObject (#108)#110
fix: handle missing content-length header in statObject (#108)#110hammadparveez wants to merge 1 commit intoxtyxtyx:masterfrom
Conversation
Replace int.parse with int.tryParse when reading the content-length response header in statObject. The header is not guaranteed to be present; the null-check operator caused an unhandled crash. Using tryParse with a null-coalescing fallback returns null gracefully, which matches the already-nullable int? size field on StatObjectResult. Fixes xtyxtyx#108 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes a runtime crash in Minio.statObject() when the server’s HEAD response omits the content-length header (issue #108), by making size parsing null-safe.
Changes:
- Replace
int.parse(...!)withint.tryParse(...)forcontent-lengthwhen buildingStatObjectResult.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| lastModified: parseRfc7231Time(resp.headers['last-modified']!), | ||
| acl: retrieveAcls ? await getObjectACL(bucket, object) : null, |
There was a problem hiding this comment.
StatObjectResult.lastModified is nullable, but statObject() force-unwraps resp.headers['last-modified']!, which will still crash if that header is absent. To align with the model’s nullability (and avoid a similar class of runtime errors), parse it conditionally and pass null when the header is missing.
| return StatObjectResult( | ||
| etag: etag, | ||
| size: int.parse(resp.headers['content-length']!), | ||
| size: int.tryParse(resp.headers['content-length'] ?? ''), |
There was a problem hiding this comment.
There are existing tests for statObject() that assert size is non-null, but this change introduces new behavior when content-length is missing (returning null instead of crashing). Please add coverage for the missing-header case; if it’s hard to reproduce against the live MinIO endpoint, consider extracting the header-parsing into a small pure helper that can be unit tested with synthetic header maps.
| return StatObjectResult( | ||
| etag: etag, | ||
| size: int.parse(resp.headers['content-length']!), | ||
| size: int.tryParse(resp.headers['content-length'] ?? ''), |
There was a problem hiding this comment.
int.tryParse(resp.headers['content-length'] ?? '') changes behavior for malformed content-length values (it will silently return null instead of throwing). Consider handling the two cases separately: return null only when the header is missing, but if it is present and not parseable, surface a clear error so callers aren’t debugging an unexpected null size.
Fixes #108