# cv::error: (-215:Assertion failed) !buf.empty() && buf.data != NULL in function 'cv::imdecode'

- **ID:** `opencv/io-imdecode-corrupt-data`
- **Domain:** opencv
- **Category:** data_error
- **Error Code:** `-215`
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

The input buffer to imdecode is empty or contains corrupt image data that cannot be decoded.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 4.7.0 | active | — | — |
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |

## Workarounds

1. **Validate the buffer before decoding: check that buf.size() > 0 and optionally check the first few bytes for a valid image signature (e.g., JPEG: 0xFF 0xD8, PNG: 0x89 0x50). Example in C++: if (buf.size() < 4 || buf[0] != 0xFF || buf[1] != 0xD8) { /* handle error */ }.** (95% success)
   ```
   Validate the buffer before decoding: check that buf.size() > 0 and optionally check the first few bytes for a valid image signature (e.g., JPEG: 0xFF 0xD8, PNG: 0x89 0x50). Example in C++: if (buf.size() < 4 || buf[0] != 0xFF || buf[1] != 0xD8) { /* handle error */ }.
   ```
2. **If reading from a network stream, use a robust HTTP client that checks Content-Length and verifies the response body is complete before passing to imdecode.** (85% success)
   ```
   If reading from a network stream, use a robust HTTP client that checks Content-Length and verifies the response body is complete before passing to imdecode.
   ```

## Dead Ends

- **Attempting to decode a buffer read from a file that was truncated mid-write** — Truncated data is still non-empty but corrupt; imdecode will fail silently or with assertion error. (90% fail)
- **Passing a buffer that was decoded from base64 without proper validation** — Base64 decoding may produce empty or invalid binary data if the input string is malformed. (85% fail)
