Back to success stories

Sample of Defect

Project Name CID Checker Category Developer Description
LabPlot 132206 NEGATIVE_RETURNS Integer handling issues Negative index used in the code could result in a totally wrong behavior in the application. The occurrence of this wrong index was not seen in the code. With the help of coverity and of its representation across several conditions (condition 1, taking false branch, etc.) it was clear and easy to fix. Thanks a lot for this check!
File: /home/alex/Projekte/labplot/src/commonfrontend/matrix/MatrixView.cpp
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
        QTextStream out(&file);

        QString sep = separator;
        sep = sep.replace(QLatin1String("TAB"), QLatin1String("\t"), Qt::CaseInsensitive);
        sep = sep.replace(QLatin1String("SPACE"), QLatin1String(" "), Qt::CaseInsensitive);

        //export values
        const int cols = m_matrix->columnCount();
        const int rows = m_matrix->rowCount();
        const QVector<QVector<double> >& matrixData = m_matrix->data();
        for (int row=0; row<rows; ++row) {
                for (int col=0; col<cols; ++col) {
                        out << matrixData[col][row];
                        if (col!=cols-1)
                                out<<sep;
                }
                out << '\n';
        }
}

void MatrixView::exportToLaTeX(const QString& path, const bool verticalHeaders, const bool horizontalHeaders,
                               const bool latexHeaders, const bool gridLines, const bool entire, const bool captions) const {

        QFile file(path);
 < 1. Condition "!file.QFile::open(QIODevice::OpenMode(QIODevice::WriteOnly | QIODevice::Truncate))", taking false branch
1089
1090
1091
1092
1093
1094
1095
1096
1097
        if (!file.open(QFile::WriteOnly | QFile::Truncate))
                return;

        QVector<QVector<QString> > toExport;

        int firstSelectedCol = 0;
        int firstSelectedRowi = 0;
        int totalRowCount = 0;
        int cols = 0;
 < 2. Condition "entire", taking false branch
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
        if (entire) {
                cols = m_matrix->columnCount();
                totalRowCount = m_matrix->rowCount();
                toExport.reserve(totalRowCount);
                toExport.resize(totalRowCount);
                for (int row = 0; row < totalRowCount; ++row) {
                        toExport[row].reserve(cols);
                        toExport[row].resize(cols);
                        for (int col = 0; col < cols; ++col)
                                toExport[row][col] = m_matrix->text(row,col);
                }
                firstSelectedCol = 0;
                firstSelectedRowi = 0;
        } else {
                cols = selectedColumnCount();
                totalRowCount = selectedRowCount();

                firstSelectedCol = firstSelectedColumn();
                const int lastSelectedCol = lastSelectedColumn();
                const int lastSelectedRowi = lastSelectedRow();
 <<< CID 132206: Integer handling issues NEGATIVE_RETURNS
 <<< 3. Function "this->firstSelectedRow(false)" returns a negative number.
 << 4. Assigning: signed variable "firstSelectedRowi" = "firstSelectedRow".
1118
1119
1120
1121
1122
1123
                firstSelectedRowi = firstSelectedRow();

                toExport.reserve(lastSelectedRowi - firstSelectedRowi+1);
                toExport.resize(lastSelectedRowi - firstSelectedRowi+1);
                int r = 0;
                int c = 0;
 << 5. Assigning: signed variable "row" = "firstSelectedRowi".
 < 6. Condition "row <= lastSelectedRowi", taking true branch
1124
1125
1126
1127
                for (int row = firstSelectedRowi; row <= lastSelectedRowi; ++row, ++r) {
                        toExport[r].reserve(lastSelectedCol - firstSelectedCol+1);
                        toExport[r].resize(lastSelectedCol - firstSelectedCol+1);
                        c = 0;
 < 7. Condition "col <= lastSelectedCol", taking true branch
1128
                        for(int col = firstSelectedCol; col <= lastSelectedCol; ++col,++c)
 <<< CID 132206: Integer handling issues NEGATIVE_RETURNS
 <<< 8. Passing variable "row" to a parameter that cannot be negative.
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
                                toExport[r][c] = m_matrix->text(row, col);
                }
        }

        int columnsStringSize = 0;
        int headerStringSize = 0;
        int columnsPerTable = 0;
        const int firstHHeaderSectionLength = m_tableView->model()->headerData(0, Qt::Horizontal).toString().length();
        const int firstSelectedVHeaderSectionLength = m_tableView->model()->headerData(firstSelectedRow(), Qt::Vertical).toString().length();
        if (verticalHeaders) {
                if (entire)
                        headerStringSize += firstHHeaderSectionLength;
                else
                        headerStringSize += firstSelectedVHeaderSectionLength;
        }
        if (!horizontalHeaders && verticalHeaders) {
                if (entire)
                        columnsStringSize += firstHHeaderSectionLength;
                else
                        columnsStringSize += firstSelectedVHeaderSectionLength;
        }

        for (int col = 0; col < cols; ++col) {
                columnsStringSize += toExport.at(0).at(col).length();
                if (horizontalHeaders)
                        headerStringSize += m_tableView->model()->headerData(col, Qt::Horizontal).toString().length();
                if ((columnsStringSize > 65) || (headerStringSize > 65))
Events:
4. var_assign MatrixView.cpp:1118
5. var_assign MatrixView.cpp:1124
3. negative_return_fn MatrixView.cpp:1118
8. negative_returns MatrixView.cpp:1129