dbzhang800 / QtXlsxWriter

.xlsx file reader and writer for Qt5
http://qtxlsx.debao.me
Other
1.24k stars 631 forks source link

qtxlxs segfault when reading an empty excel file using xlsx.cellAt(0,0)->value(); #179

Closed cdwijs closed 4 years ago

cdwijs commented 6 years ago

Hi All,

I've made a program that segfaults when I run this line on an empty excel file: variant = xlsx.cellAt(0,0)->value(); But it does not crash using this line: (although it yields an invalid variant) variant = xlsx.read(0,0);

I've traced the problem back to QVariant Cell::value(), d is not accessible.

My versions: Windows 7 Enterprise SP1 Qt 5.11.1 mingw53_32

Cheers, Cedric

mainwindow.cpp `

include "mainwindow.h"

include "ui_mainwindow.h"

include "xlsxdocument.h"

include "xlsxchart.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { QString filename = "u:/test.xlsx";

ui->setupUi(this);

QXlsx::Document xlsx1;
xlsx1.write(1,1,"Hi there!");
xlsx1.saveAs(filename); //create empty file

QXlsx::Document xlsx(filename);
QVariant variant;
variant = xlsx.cellAt(0,0)->value(); //crash
variant = xlsx.read(0,0); //no crash

}

MainWindow::~MainWindow() { delete ui; } .pro

-------------------------------------------------

#

Project created by QtCreator 2018-10-30T17:59:42

#

-------------------------------------------------

QT += core gui widgets

TARGET = xlsx-crash TEMPLATE = app

The following define makes your compiler emit warnings if you use

any feature of Qt which has been marked as deprecated (the exact warnings

depend on your compiler). Please consult the documentation of the

deprecated API in order to know how to port your code away from it.

DEFINES += QT_DEPRECATED_WARNINGS

You can also make your code fail to compile if you use deprecated APIs.

In order to do so, uncomment the following line.

You can also select to disable deprecated APIs only up to a certain version of Qt.

DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

include(3rdparty/QtXlsxWriter/qtxlsx.pri)

CONFIG += c++11

SOURCES += \ main.cpp \ mainwindow.cpp

HEADERS += \ mainwindow.h

FORMS += \ mainwindow.ui

Default rules for deployment.

qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target `

cdwijs commented 6 years ago

This is correct behavior from xlsx. I should have checked for a 0 pointer like this: QXlsx::Cell *cell; cell = xlsx.cellAt(2,2); //cell = 0 on empty cell if (cell) { variant = cell->value(); } Cheers, Cedric

cdwijs commented 6 years ago

`

QXlsx::Cell *cell;

cell = xlsx.cellAt(2,2); //cell = 0 on empty cell

if (cell)

{

    variant = cell->value();

}

`