{"record":{"id":"0b378e54dfb2cee5","repo":"gohugoio/hugo","slug":"malloc-failed-for-final-webp-data-n","errorCode":null,"errorMessage":"malloc failed for final webp data\\n","messagePattern":"malloc failed for final webp data\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"internal/warpc/genwebp/webp.c","lineNumber":273,"sourceCode":"        WebPAnimEncoderDelete(enc);\n        return NULL;\n    }\n\n    WebPData webp_data_out;\n    WebPDataInit(&webp_data_out);\n    if (!WebPAnimEncoderAssemble(enc, &webp_data_out))\n    {\n        fprintf(stderr, \"WebPAnimEncoderAssemble failed\\n\");\n        WebPAnimEncoderDelete(enc);\n        return NULL;\n    }\n    WebPAnimEncoderDelete(enc);\n\n    *output_size = webp_data_out.size;\n    uint8_t *webp_data = malloc(*output_size);\n    if (webp_data == NULL)\n    {\n        fprintf(stderr, \"malloc failed for final webp data\\n\");\n        WebPDataClear(&webp_data_out);\n        return NULL;\n    }\n    memcpy(webp_data, webp_data_out.bytes, *output_size);\n    WebPDataClear(&webp_data_out);\n\n    return webp_data;\n}\n\nstatic uint8_t initDecoderConfig(WebPDecoderConfig *config, WebPData data)\n{\n    if (!WebPInitDecoderConfig(config))\n    {\n        return 0;\n    }\n    if (WebPGetFeatures(data.bytes, data.size, &config->input) != VP8_STATUS_OK)\n    {\n        return 0;","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/internal/warpc/genwebp/webp.c#L255-L291","documentation":"Printed by genwebp's encodeNRGBAAnimated (webp.c:273) when malloc fails to allocate a buffer to copy the assembled WebPData into Hugo-owned memory. The animation was encoded and assembled successfully, but the final memcpy destination cannot be allocated. The worker clears the WebPData and returns NULL, so the encoded animation is lost. This is a pure out-of-memory condition.","triggerScenarios":"The assembled WebP output is large enough that a same-sized malloc fails (the encoder already holds a copy, so peak memory is roughly 2x output size), or the process is under overall memory pressure from concurrent work.","commonSituations":"Large animated WebP output (tens of MB), workers processing multiple animations concurrently under a tight cgroup, or a memory leak in an earlier iteration that has consumed available heap.","solutions":["Raise the worker memory limit so peak (encoder buffer + copied buffer) fits.","Reduce animation size to shrink the output.","Serialize animated encodes so only one lives in memory at a time.","Fix any upstream leak that reduces available heap before this allocation.","Consider patching genwebp to hand off the WebPData bytes directly instead of malloc+memcpy (avoiding the 2x peak)."],"exampleFix":"// before (conceptual genwebp path): encode then duplicate\nuint8_t *webp_data = malloc(webp_data_out.size); // fails for large outputs\nmemcpy(webp_data, webp_data_out.bytes, webp_data_out.size);\n\n// after: hand off ownership of webp_data_out.bytes to the caller and skip the copy,\n// OR stream the bytes out via write_blob in chunks instead of one malloc","handlingStrategy":"fallback","validationCode":"// Reject animations whose estimated output would exceed available memory.\nfunc canFitAssembled(available, w, h, frames int) bool {\n    // crude upper bound: assume ~1 byte/pixel per frame for WebP\n    est := w * h * frames\n    return est*2 < available // 2x for encoder + copy\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Raise the worker memory limit so the encoder buffer and its copy both fit.","Serialize animated encodes per worker so only one lives in memory.","Reduce animation size (frames or resolution) for memory-constrained deployments.","Consider patching genwebp to hand off WebPData bytes directly instead of malloc+memcpy, halving peak memory."],"tags":["webp","animation","oom","malloc","encoding"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}