{"record":{"id":"8b6e4b6d38e9add0","repo":"gohugoio/hugo","slug":"webppictureimportrgba-failed-d-s-n","errorCode":null,"errorMessage":"WebPPictureImportRGBA failed: %d (%s)\\n","messagePattern":"WebPPictureImportRGBA failed: (.+?) \\((.+?)\\)\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/warpc/genwebp/webp.c","lineNumber":138,"sourceCode":"\n    pic.use_argb = 1;\n    pic.width = width;\n    pic.height = height;\n    pic.writer = WebPMemoryWrite;\n    pic.custom_ptr = &wrt;\n    WebPMemoryWriterInit(&wrt);\n    ok = WebPPictureImportRGBA(&pic, rgba, stride);\n    if (ok)\n    {\n        ok = WebPEncode(config, &pic);\n        if (!ok)\n        {\n            fprintf(stderr, \"WebPEncode failed: %d (%s)\\n\", pic.error_code, kErrorMessages[pic.error_code]);\n        }\n    }\n    else\n    {\n        fprintf(stderr, \"WebPPictureImportRGBA failed: %d (%s)\\n\", pic.error_code, kErrorMessages[pic.error_code]);\n    }\n    WebPPictureFree(&pic);\n    if (!ok)\n    {\n        WebPMemoryWriterClear(&wrt);\n        return NULL;\n    }\n    *output_size = wrt.size;\n    return wrt.mem;\n}\n\nstatic uint8_t *encodeGray(WebPConfig *config, uint8_t *y, int width, int height, int stride, size_t *output_size)\n{\n    WebPPicture pic;\n    WebPMemoryWriter wrt;\n\n    int ok;\n    if (!WebPPictureInit(&pic))","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/internal/warpc/genwebp/webp.c#L120-L156","documentation":"Printed by genwebp's encodeNRGBA (webp.c:138) when WebPPictureImportRGBA returns 0. This step copies the caller's RGBA buffer into libwebp's internal WebPPicture and validates that stride >= width*4 and dimensions are within WebP's limits. Failure most commonly means BAD_DIMENSION (width/height over 16383, or non-positive) or a stride smaller than width*4 bytes. The printed error_code and message pinpoint the cause. The function returns NULL.","triggerScenarios":"Calling encodeNRGBA with width or height > 16383, with stride < width*4, with width/height <= 0, or with a NULL/short rgba buffer. Also if the rgba pointer's allocation is smaller than stride*height bytes (buffer underrun detected by libwebp's internal checks).","commonSituations":"Hugo passes a stride computed for RGB (3 bytes) when the image is RGBA (4 bytes), an image was resized to dimensions above the WebP cap, an 8-bit grayscale path accidentally routed into the RGBA encoder with the wrong stride, or a corrupt/short pixel buffer from a prior decode step.","solutions":["Assert on the Go side that stride == width*4 and that the pixel buffer length is at least stride*height before invoking encodeNRGBA.","Clamp width and height to <= 16383 (downscale or reject the WebP output format).","Reject zero/negative width or height before sending the command.","Verify the rgba buffer actually contains stride*height bytes — re-read the upstream decode output if there is any doubt.","Cross-check that the Go image type really is NRGBA/RGBA and not a premultiplied-alpha format with a different stride."],"exampleFix":"// before: stride computed for 3-byte RGB on an RGBA image\nstride := img.Bounds().Dx() * 3\n\n// after: stride must be width*4 for RGBA import\nstride := img.Bounds().Dx() * 4\npix := make([]byte, stride*img.Bounds().Dy())\n// fill pix, then send to genwebp","handlingStrategy":"validation","validationCode":"// Assert stride and buffer length match WebP RGBA expectations.\nfunc validateRGBAForWebp(pix []byte, w, h, stride int) error {\n    if w <= 0 || h <= 0 { return fmt.Errorf(\"invalid %dx%d\", w, h) }\n    if stride < w*4 { return fmt.Errorf(\"stride %d < width*4 %d\", stride, w*4) }\n    if len(pix) < stride*h { return fmt.Errorf(\"buffer %d < stride*height %d\", len(pix), stride*h) }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always compute stride as width*4 for RGBA before encoding.","Confirm the Go image type is NRGBA/RGBA (not premultiplied alpha) and matches the buffer layout.","Clamp dimensions to <= 16383.","Re-read the upstream decode output if there is any doubt about the buffer's true length."],"tags":["webp","encoding","stride","dimensions","rgba"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}