{"record":{"id":"fa141536395ef3a4","repo":"Yalantis/uCrop","slug":"cimgdisplay-screen-width-failed-to-open-x11-di","errorCode":null,"errorMessage":"CImgDisplay::screen_width(): Failed to open X11 display.","messagePattern":"CImgDisplay::screen_width\\(\\): Failed to open X11 display\\.","errorType":"exception","errorClass":"CImgDisplayException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":10544,"sourceCode":"              colormap[index].green = (unsigned short)(g<<8);\n              colormap[index].blue = (unsigned short)(b<<8);\n              colormap[index++].flags = DoRed | DoGreen | DoBlue;\n            }\n      }\n      }\n      XStoreColors(X11_attr.display,cmap,colormap,256);\n      delete[] colormap;\n    }\n\n    // Public functions.\n    static int screen_width() {\n      cimg::X11_attr &X11_attr = cimg::X11_attr::ref();\n      Display *const dpy = X11_attr.display;\n      int res = 0;\n      if (!dpy) {\n        Display *const _dpy = XOpenDisplay(0);\n        if (!_dpy)\n          throw CImgDisplayException(\"CImgDisplay::screen_width(): Failed to open X11 display.\");\n        res = DisplayWidth(_dpy,DefaultScreen(_dpy));\n        XCloseDisplay(_dpy);\n      } else {\n\n#ifdef cimg_use_xrandr\n        if (X11_attr.resolutions && X11_attr.curr_resolution)\n          res = X11_attr.resolutions[X11_attr.curr_resolution].width;\n        else res = DisplayWidth(dpy,DefaultScreen(dpy));\n#else\n        res = DisplayWidth(dpy,DefaultScreen(dpy));\n#endif\n      }\n      return res;\n    }\n\n    static int screen_height() {\n      cimg::X11_attr &X11_attr = cimg::X11_attr::ref();\n      Display *const dpy = X11_attr.display;","sourceCodeStart":10526,"sourceCodeEnd":10562,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L10526-L10562","documentation":"CImgDisplay::screen_width() queries the X server for the screen width; if no display is currently open it tries XOpenDisplay(NULL) itself, and throws CImgDisplayException when that connection fails. Same root causes as [26] but triggered by the static screen-size query.","triggerScenarios":"Calling cimg::CImgDisplay::screen_width() (or screen_height()) in an environment with no reachable X server: DISPLAY unset/invalid, headless CI/container, SSH without forwarding, or X server down.","commonSituations":"Code that queries screen size at startup to size windows, running on headless servers or in Docker; cron/systemd jobs without DISPLAY; Wayland sessions without XWayland.","solutions":["Ensure DISPLAY is set and an X server is reachable before calling (xset -q as a probe)","Use Xvfb in headless/CI environments: Xvfb :99 & export DISPLAY=:99","Avoid the query in headless mode — hardcode/default the window size or read resolution from your own config"],"exampleFix":"// before\nint w = cimg::CImgDisplay::screen_width(); // throws headless\n// after\nconst char *dpy = getenv(\"DISPLAY\");\nint w = (dpy && *dpy) ? cimg::CImgDisplay::screen_width() : 1280; // fallback size","handlingStrategy":"try-catch","validationCode":"const char *d = getenv(\"DISPLAY\");\nbool xReady = d && *d && (system(\"xset -q > /dev/null 2>&1\") == 0);","typeGuard":"bool canQueryScreen() { const char *d = getenv(\"DISPLAY\"); return d && *d; }","tryCatchPattern":"try { w = cimg::CImgDisplay::screen_width(); } catch (const cimg_library::CImgDisplayException &e) { w = 1280; // default fallback size\n}","preventionTips":["Verify DISPLAY before any static CImgDisplay query","Provide sensible default window dimensions for headless environments","Start Xvfb in CI jobs that exercise display code","Do not call screen_width()/screen_height() at process startup on servers"],"tags":["x11","display","environment","headless"],"backgroundTag":"missing-env-var","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}