Bug Summary

File:librecad/src/lib/math/lc_convert.cpp
Warning:line 182, column 5
Undefined or garbage value returned to caller

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name lc_convert.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/runner/work/LibreCAD/LibreCAD/librecad/src -fcoverage-compilation-dir=/home/runner/work/LibreCAD/LibreCAD/librecad/src -resource-dir /usr/lib/llvm-18/lib/clang/18 -D _REENTRANT -D DWGSUPPORT -D MUPARSER_STATIC -D QC_APPDIR="librecad" -D LC_VERSION=2.2.2_alpha1-607-g330d41cec -D LC_PRERELEASE=true; -D QT_NO_DEBUG -D QT_SVG_LIB -D QT_PRINTSUPPORT_LIB -D QT_WIDGETS_LIB -D QT_GUI_LIB -D QT_NETWORK_LIB -D QT_CORE_LIB -I . -I /usr/include -I ../../libraries/lciconengine -I ../../libraries/libdxfrw/src -I ../../libraries/jwwlib/src -I ../../libraries/shapelib/src -I cmd -I lib/actions -I lib/actions/visual_snap -I lib/actions/options -I lib/creation -I lib/debug -I lib/engine -I lib/engine/document -I lib/engine/document/blocks -I lib/engine/document/container -I lib/engine/document/dimstyles -I lib/engine/document/entities -I lib/engine/document/entities/support -I lib/engine/document/fonts -I lib/engine/document/io -I lib/engine/document/layers -I lib/engine/document/patterns -I lib/engine/document/selection -I lib/engine/document/textstyles -I lib/engine/document/ucs -I lib/engine/document/variables -I lib/engine/document/views -I lib/engine/clipboard -I lib/engine/overlays -I lib/engine/overlays/angles_base -I lib/engine/overlays/highlight -I lib/engine/overlays/preview -I lib/engine/overlays/references -I lib/engine/overlays/crosshair -I lib/engine/overlays/info_cursor -I lib/engine/overlays/overlay_box -I lib/engine/overlays/ucs_mark -I lib/engine/undo -I lib/engine/utils -I lib/engine/settings -I lib/fileio -I lib/filters -I lib/generators -I lib/generators/makercamsvg -I lib/generators/layers -I lib/generators/image -I lib/gui -I lib/gui/grid -I lib/gui/render -I lib/gui/render/headless -I lib/gui/render/widget -I lib/information -I lib/math -I lib/modification -I lib/selection -I lib/selection/metaentity -I lib/selection/metaentity/entities -I lib/printing -I lib/properties -I actions -I actions/dock_widgets -I actions/dock_widgets/block -I actions/dock_widgets/entity_info -I actions/dock_widgets/layer -I actions/dock_widgets/library -I actions/dock_widgets/ucs_list -I actions/drawing -I actions/drawing/draw -I actions/drawing/draw/arc -I actions/drawing/draw/circle -I actions/drawing/draw/curve -I actions/drawing/draw/spline -I actions/drawing/draw/dimensions -I actions/drawing/draw/ellipse -I actions/drawing/draw/hatch -I actions/drawing/draw/image -I actions/drawing/draw/line -I actions/drawing/draw/point -I actions/drawing/draw/rect -I actions/drawing/draw/polygon -I actions/drawing/draw/misc -I actions/drawing/draw/line/shapes -I actions/drawing/draw/line/misc -I actions/drawing/draw/line/shapes/rect -I actions/drawing/draw/line/shapes/polygon -I actions/drawing/draw/polyline -I actions/drawing/draw/text -I actions/drawing/edit -I actions/drawing/info -I actions/drawing/pick -I actions/drawing/modify -I actions/drawing/pen -I actions/drawing/rel_zero -I actions/drawing/selection -I actions/drawing/snap -I actions/drawing/zoom -I actions/file -I actions/options -I actions/print_preview -I ui -I ui/action_options -I ui/action_options/circle -I ui/action_options/curve -I ui/action_options/spline -I ui/action_options/dimensions -I ui/action_options/edit -I ui/action_options/image -I ui/action_options/info -I ui/action_options/insert -I ui/action_options/line -I ui/action_options/rect -I ui/action_options/polygon -I ui/action_options/misc -I ui/action_options/modify -I ui/action_options/ellipse -I ui/action_options/other -I ui/action_options/polyline -I ui/action_options/point -I ui/action_options/print_preview -I ui/action_options/selection -I ui/action_options/snap -I ui/action_options/text -I ui/actions -I ui/components -I ui/components/relative_position_assistant -I ui/components/comboboxes -I ui/components/containers -I ui/components/creators -I ui/components/layouts -I ui/components/pen -I ui/components/status_bar -I ui/components/toolbars -I ui/components/utils -I ui/dialogs -I ui/dialogs/actions -I ui/dialogs/actions/modify -I ui/dialogs/actions/quick_selection -I ui/dialogs/modify -I ui/dialogs/entity -I ui/dialogs/creators -I ui/dialogs/file -I ui/dialogs/file/export -I ui/dialogs/file/export/layers -I ui/dialogs/file/export/image -I ui/dialogs/file/export/makercam -I ui/dialogs/main -I ui/dialogs/settings -I ui/dialogs/settings/dimstyles -I ui/dialogs/settings/dimstyles/dimstyle_manager -I ui/dialogs/settings/dimstyles/dimstyle_manager/support -I ui/dialogs/settings/options_device -I ui/dialogs/settings/options_drawing -I ui/dialogs/settings/options_general -I ui/dialogs/settings/options_widget -I ui/dialogs/settings/shortcuts -I ui/dock_widgets -I ui/dock_widgets/block_widget -I ui/dock_widgets/command_line -I ui/dock_widgets/entity_info -I ui/dock_widgets/layer_widget -I ui/dock_widgets/layers_tree -I ui/dock_widgets/library_widget -I ui/dock_widgets/pen_palette -I ui/dock_widgets/pen_wizard -I ui/dock_widgets/property_sheet -I ui/dock_widgets/property_sheet/lib -I ui/dock_widgets/property_sheet/lib/properties -I ui/dock_widgets/property_sheet/lib/view -I ui/dock_widgets/property_sheet/lib/view/edit -I ui/dock_widgets/property_sheet/lib/widgets -I ui/dock_widgets/property_sheet/lib/widgets/sheet -I ui/dock_widgets/property_sheet/metaentity -I ui/dock_widgets/property_sheet/metaentity/entities -I ui/dock_widgets/property_sheet/metaentity/entities/document -I ui/dock_widgets/property_sheet/properties -I ui/dock_widgets/property_sheet/properties/action -I ui/dock_widgets/property_sheet/properties/bool -I ui/dock_widgets/property_sheet/properties/color -I ui/dock_widgets/property_sheet/properties/double -I ui/dock_widgets/property_sheet/properties/enum -I ui/dock_widgets/property_sheet/properties/int -I ui/dock_widgets/property_sheet/properties/layer -I ui/dock_widgets/property_sheet/properties/linetype -I ui/dock_widgets/property_sheet/properties/linewidth -I ui/dock_widgets/property_sheet/properties/rect -I ui/dock_widgets/property_sheet/properties/rsvector -I ui/dock_widgets/property_sheet/properties/string -I ui/dock_widgets/views_list -I ui/dock_widgets/ucs_list -I ui/dock_widgets/workspaces -I ui/dock_widgets/cad -I ui/main -I ui/main/init -I ui/main/persistence -I ui/main/release_check -I ui/main/support -I ui/main/fontviewer -I ui/main/workspaces -I ui/view -I main -I main/console_dxf2pdf -I test -I plugins -I ../res -I ../res/arrows -I ../res/controls -I ../res/dxf -I ../res/gdt -I ../res/icons -I ../res/images -I ../../../Qt/6.9.0/gcc_64/include -I ../../../Qt/6.9.0/gcc_64/include/QtSvg -I ../../../Qt/6.9.0/gcc_64/include/QtPrintSupport -I ../../../Qt/6.9.0/gcc_64/include/QtWidgets -I ../../../Qt/6.9.0/gcc_64/include/QtGui -I ../../../Qt/6.9.0/gcc_64/include/QtNetwork -I ../../../Qt/6.9.0/gcc_64/include/QtCore -I ../../generated/librecad/moc -I ../../generated/librecad/ui -I ../../../Qt/6.9.0/gcc_64/mkspecs/linux-g++ -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -std=gnu++1z -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/runner/work/LibreCAD/LibreCAD/out/2026-08-04-154929-5069-1 -x c++ lib/math/lc_convert.cpp
1/*******************************************************************************
2 *
3 This file is part of the LibreCAD project, a 2D CAD program
4
5 Copyright (C) 2024 LibreCAD.org
6 Copyright (C) 2024 sand1024
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 ******************************************************************************/
22
23#include "lc_convert.h"
24
25#include <QRegularExpression>
26
27#include "lc_linemath.h"
28#include "rs_debug.h"
29#include "rs_math.h"
30#include "rs_settings.h"
31#include "rs_units.h"
32
33// fixme - sand - review the entire codebase and insure uniform string/double and vice-versa conversion
34QString LC_Convert::asString(double value, const int precision){
35 if (LC_LineMath::isNotMeaningful(value)){
36 value = 0.0;
37 }
38 return QString::number(value, 'g', precision);
39}
40
41QString LC_Convert::asStringAngle(double value, const int precision){
42 if (!LC_LineMath::isMeaningfulAngle(value)){
43 value = 0.0;
44 }
45 return QString::number(value, 'g', precision);
46}
47
48QString LC_Convert::asStringAngleDeg(double value, const int precision){
49 if (!LC_LineMath::isMeaningfulAngle(value)){
50 value = 0.0;
51 }
52 const double angleDeg = RS_Math::rad2deg(value);
53 return QString::number(angleDeg, 'g', precision);
54}
55
56
57/**
58 * Just utility method for conversion of provided string to double value.
59 * @param strValue string that represents value
60 * @param res result of conversion
61 * @param notMeaningful value that should be returned if given string denotes non-meaningful value (less than tolerance)
62 * @param positiveOnly if true, positive value (via std::abs()) will be always returned, false - otherwise.
63 * @return true if string was converted without errors
64 */
65bool LC_Convert::toDouble(const QString& strValue, double &res, const double notMeaningful, const bool positiveOnly){
66 bool ok = false;
67 const double x = RS_Math::eval(strValue, &ok);
68 if(ok){
69 res = LC_LineMath::getMeaningful(x, notMeaningful);
70 if (positiveOnly){
71 res = std::abs(res);
72 }
73 }
74 return ok;
75}
76
77/**
78 * Just utility method for conversion of provided string to double value, given that double is for angle
79 * @param strValue string that represents value
80 * @param res result of conversion
81 * @param notMeaningful value that should be returned if given string denotes non-meaningful value (less than tolerance)
82 * @param positiveOnly if true, positive value (via std::abs()) will be always returned, false - otherwise.
83 * @return true if string was converted without errors
84 */
85bool LC_Convert::toDoubleAngleRad(const QString& strValue, double &res, const double notMeaningful, const bool positiveOnly){
86 double radValue;
87 const bool result = parseToToDoubleAngleDegrees(strValue, radValue, notMeaningful, positiveOnly);
88 if (result){
89 const double resRad = RS_Math::deg2rad(radValue);
90 res = RS_Math::correctAngle(resRad);
91 }
92 return result;
93}
94
95
96bool LC_Convert::parseToToDoubleAngleDegrees(const QString& strValue, double &res, const double notMeaningful, const bool positiveOnly){
97 bool ok = false;
98 const bool doNotAllowNonDecimalAnglesInput = LC_GET_ONE_BOOLRS_Settings::instance()->readBoolSingle("CADPreferences", "InputAnglesAsDecimalsOnly", false);
99 double angleDegrees;
100 // cleanup degree size, if any
101 if (doNotAllowNonDecimalAnglesInput){
3
Assuming 'doNotAllowNonDecimalAnglesInput' is true
4
Taking true branch
102 QString stringToEval = strValue;
103 stringToEval = stringToEval.remove( QChar(0xB0));
104 angleDegrees = RS_Math::eval(stringToEval, &ok);
105 }
106 else{
107 QString stringToEval = strValue;
108 stringToEval = stringToEval.remove( QChar(0xB0));
109 stringToEval = RS_Units::replaceAllPotentialAnglesByDecimalDegrees(stringToEval, &ok);
110 angleDegrees = RS_Math::eval(stringToEval, &ok);
111 }
112 if(ok){
5
Assuming 'ok' is false
6
Taking false branch
113 res = LC_LineMath::getMeaningfulAngle(angleDegrees, notMeaningful);
114 if (positiveOnly){
115 res = std::abs(res);
116 }
117 }
118 return ok;
7
Returning without writing to 'res'
119}
120
121/**
122 * @{description} Update a length string to support fraction
123 * (1 1/2") to (1+1/2")
124 * (1"1/2) to (1+1/2")
125 */
126QString LC_Convert::updateForFraction(QString input) {
127 // if the expression is already valid, bypass fraction processing
128 bool okay = false;
129 const double value = RS_Math::eval(input, &okay);
130 if (okay) {
131 return QString{}.setNum(value, 'g', 10);
132 }
133
134 // support fraction at the end: (1'1/2) => (1 1/2')
135 static const QRegularExpression rx{R"((\D*)([\d]+)\s*(['"])([\d]+)/([\d]+)\s*$)"};
136
137 const QRegularExpressionMatch match = rx.match(input);
138 if (match.hasMatch()) {
139 const qsizetype pos = match.capturedStart();
140 input = input.left(pos) + match.captured(1) + match.captured(2) + " " + match.captured(4) + "/" +
141 match.captured(5) + match.captured(3);
142 }
143 static std::array<std::tuple<QRegularExpression, int, int>, 3> regexps{
144 {{QRegularExpression{R"((\D*)([\d]+)\s+([\d]+)/([\d]+)\s*([\D$]))"},3, 5},
145 {QRegularExpression{R"((\D*)([\d]+)\s+([\d]+)/([\d]+)\s*(['"]))"},3, 5},
146 {QRegularExpression{R"((\D*)\s*([\d]+)/([\d]+)\s*([\D$]))"},2, 4},}};
147
148 LC_LOGRS_Debug::Log() << "input=" << input;
149 static QRegularExpression expression(R"(\s+)");
150 for (auto &[regx, index, tailI]: regexps) {
151 input = evaluateFraction(input, regx, index, tailI).replace(expression, QString{});
152 }
153 LC_LOGRS_Debug::Log() << "eval: " << input;
154 return input;
155}
156
157QString LC_Convert::evaluateFraction(QString input, const QRegularExpression& rx, const int index, const int tailI) {
158 const QString copy = input;
159 const QRegularExpressionMatch match = rx.match(copy);
160
161 if (match.hasMatch()) {
162 const qsizetype pos = match.capturedStart();
163 LC_ERRRS_Debug::Log(RS_Debug::D_ERROR) << "Evaluate: " << copy;
164 const QString formula = ((index != 2) ? match.captured(2) + "+" : QString{}) + match.captured(index) + "/" +
165 match.captured(index + 1);
166 LC_ERRRS_Debug::Log(RS_Debug::D_ERROR) << "formula=" << formula;
167 const QString value = QString{}.setNum(RS_Math::eval(formula), 'g', 10);
168 LC_ERRRS_Debug::Log(RS_Debug::D_ERROR) << "formula=" << formula << ": value=" << value;
169
170 const QString tail = QString{R"(\)"} + QString::number(tailI);
171
172 return input.left(pos)
173 + input.mid(pos, match.capturedLength()).replace(rx, R"( \1)" + value + tail)
174 + evaluateFraction(input.right(input.size() - pos - match.capturedLength()), rx, index, tailI);
175 }
176 return input;
177}
178[[deprecated]] // fixme - garbase value if doNotAllow is true and ok is valse?
179double LC_Convert::evalAngleValue(const QString& angleStr, bool& ok2) {
180 double angleDegrees;
1
'angleDegrees' declared without an initial value
181 ok2 = parseToToDoubleAngleDegrees(angleStr, angleDegrees, 0.0, false);
2
Calling 'parseToToDoubleAngleDegrees'
8
Returning from 'parseToToDoubleAngleDegrees'
182 return angleDegrees;
9
Undefined or garbage value returned to caller
183}
184
185bool LC_Convert::toInt(const QString& strVal, int& res) {
186 bool ok;
187 res = strVal.toInt(&ok);
188 return ok;
189}