{"record":{"id":"4586e71349c0fbc4","repo":"Yalantis/uCrop","slug":"cimg-s-dijkstra-specified-index-of-starting","errorCode":null,"errorMessage":"CImg<%s>::dijkstra(): Specified index of starting node %u is higher than number of nodes %u.","messagePattern":"CImg<(.+?)>::dijkstra\\(\\): Specified index of starting node %u is higher than number of nodes %u\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":33856,"sourceCode":"    }\n\n    //! Compute minimal path in a graph, using the Dijkstra algorithm.\n    /**\n       \\param distance An object having operator()(unsigned int i, unsigned int j) which returns distance\n         between two nodes (i,j).\n       \\param nb_nodes Number of graph nodes.\n       \\param starting_node Index of the starting node.\n       \\param ending_node Index of the ending node (set to ~0U to ignore ending node).\n       \\param previous_node Array that gives the previous node index in the path to the starting node\n         (optional parameter).\n       \\return Array of distances of each node to the starting node.\n    **/\n    template<typename tf, typename t>\n    static CImg<T> dijkstra(const tf& distance, const unsigned int nb_nodes,\n                            const unsigned int starting_node, const unsigned int ending_node,\n                            CImg<t>& previous_node) {\n      if (starting_node>=nb_nodes)\n        throw CImgArgumentException(\"CImg<%s>::dijkstra(): Specified index of starting node %u is higher \"\n                                    \"than number of nodes %u.\",\n                                    pixel_type(),starting_node,nb_nodes);\n      CImg<T> dist(1,nb_nodes,1,1,cimg::type<T>::max());\n      dist(starting_node) = 0;\n      previous_node.assign(1,nb_nodes,1,1,(t)-1);\n      previous_node(starting_node) = (t)starting_node;\n      CImg<uintT> Q(nb_nodes);\n      cimg_forX(Q,u) Q(u) = (unsigned int)u;\n      cimg::swap(Q(starting_node),Q(0));\n      unsigned int sizeQ = nb_nodes;\n      while (sizeQ) {\n        // Update neighbors from minimal vertex.\n        const unsigned int umin = Q(0);\n        if (umin==ending_node) sizeQ = 0;\n        else {\n          const T dmin = dist(umin);\n          const T infty = cimg::type<T>::max();\n          for (unsigned int q = 1; q<sizeQ; ++q) {","sourceCodeStart":33838,"sourceCodeEnd":33874,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L33838-L33874","documentation":"CImg's static dijkstra() computes shortest paths over nb_nodes nodes indexed 0..nb_nodes-1. If starting_node >= nb_nodes the starting index is out of range and CImgArgumentException is thrown before any traversal.","triggerScenarios":"Calling CImg<T>::dijkstra(distance, nb_nodes, starting_node, ending_node, previous_node) with starting_node >= nb_nodes — typically an off-by-one (passing nb_nodes itself) or an unvalidated node id.","commonSituations":"1-based node ids passed to the 0-based API; node id read from user input/file without bounds check; nb_nodes computed from a smaller graph than the ids refer to.","solutions":["Validate starting_node < nb_nodes (and ideally ending_node < nb_nodes) before calling","Convert 1-based ids to 0-based: call with starting_node - 1 if your ids start at 1","Check the source of nb_nodes matches the graph actually being traversed"],"exampleFix":"// before\nunsigned start = nodeId; // 1-based, can be == nb_nodes\nCImg<float> path = CImg<float>::dijkstra(dist, n, start, end, prev);\n// after\nif (nodeId >= 1 && nodeId <= n) {\n  CImg<float> path = CImg<float>::dijkstra(dist, n, nodeId - 1, end, prev);\n}","handlingStrategy":"validation","validationCode":"if (starting_node >= nb_nodes || ending_node >= nb_nodes) throw std::out_of_range(\"node index must be < nb_nodes\");","typeGuard":"bool isValidNode(unsigned node, unsigned nb_nodes) { return node < nb_nodes; }","tryCatchPattern":"try { CImg<T> path = CImg<T>::dijkstra(dist, n, s, e, prev); } catch (CImgArgumentException& e) { /* clamp/reject node id */ }","preventionTips":["Remember node ids are 0-based; convert 1-based inputs","Validate node ids at the API boundary (user input, files)","Keep nb_nodes consistent with the actual graph size"],"tags":["cimg","dijkstra","graph","index-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}