{"record":{"id":"c27c61d6f3fc5daa","repo":"DrKLO/Telegram","slug":"s-crop-dimensions-exceed-image-dimensions-d-x","errorCode":null,"errorMessage":"%s: crop dimensions exceed image dimensions %d x %d\\n","messagePattern":"(.+?): crop dimensions exceed image dimensions (.+?) x (.+?)\\\\n","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/djpeg.c","lineNumber":727,"sourceCode":"      (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);\n    }\n    jpeg_skip_scanlines(&cinfo, skip_end - skip_start + 1);\n    while (cinfo.output_scanline < cinfo.output_height) {\n      num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,\n                                          dest_mgr->buffer_height);\n      (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);\n    }\n\n  /* Decompress a subregion */\n  } else if (crop) {\n    JDIMENSION tmp;\n\n    /* Check for valid crop dimensions.  We cannot check these values until\n     * after jpeg_start_decompress() is called.\n     */\n    if (crop_x + crop_width > cinfo.output_width ||\n        crop_y + crop_height > cinfo.output_height) {\n      fprintf(stderr, \"%s: crop dimensions exceed image dimensions %d x %d\\n\",\n              progname, cinfo.output_width, cinfo.output_height);\n      exit(EXIT_FAILURE);\n    }\n\n    jpeg_crop_scanline(&cinfo, &crop_x, &crop_width);\n    if (dest_mgr->calc_buffer_dimensions)\n      (*dest_mgr->calc_buffer_dimensions) (&cinfo, dest_mgr);\n    else\n      ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);\n\n    /* Write output file header.  This is a hack to ensure that the destination\n     * manager creates an output image of the proper size.\n     */\n    tmp = cinfo.output_height;\n    cinfo.output_height = crop_height;\n    (*dest_mgr->start_output) (&cinfo, dest_mgr);\n    cinfo.output_height = tmp;\n","sourceCodeStart":709,"sourceCodeEnd":745,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/djpeg.c#L709-L745","documentation":"The -crop region extends beyond the decompressed image: crop_x + crop_width exceeds output_width, or crop_y + crop_height exceeds output_height. Checked after jpeg_start_decompress(). Fatal: exits with failure.","triggerScenarios":"Passing -crop WxH+X+Y (or equivalent) where the rectangle's far corner lies outside the image. Because the check needs output_width/output_height, it can only fire at runtime after the header is parsed.","commonSituations":"Reusing crop geometry from a larger source image, an image was rotated/transposed upstream changing dimensions, off-by-one in computing X/Y from a grid, or a UI tile request beyond bounds.","solutions":["Read output_width/output_height from jpeg_read_header and compute crop so that X+W <= width and Y+H <= height.","If a tile exceeds bounds, clamp the tile size or reject the request at the caller level.","Use jpeg_crop_scanline via the API so the library adjusts the crop, rather than hardcoding raw geometry.","Recompute crop coordinates whenever the input image's dimensions may have changed."],"exampleFix":"// before\n./djpeg -crop 1000x1000+0+0 small.jpg > out.ppm\n// after: fit crop inside actual size\n# width=640 height=480 -> clamp\n./djpeg -crop 640x480+0+0 small.jpg > out.ppm","handlingStrategy":"validation","validationCode":"#include <jpeglib.h>\nint crop_ok(j_decompress_ptr c, unsigned x, unsigned y, unsigned w, unsigned h) {\n  return (x + w) <= c->output_width && (y + h) <= c->output_height;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Read output_width/output_height before computing crop geometry.","Clamp tiles to image bounds at the request layer.","Prefer jpeg_crop_scanline so the library adjusts the rectangle."],"tags":["djpeg","crop","geometry","validation","mozjpeg"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}