Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qtxlxs segfault when reading an empty excel file using xlsx.cellAt(0,0)->value();

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cdwijs
    wrote on last edited by cdwijs
    #1

    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.

    This problem is not mentioned in the bugtracker:
    https://github.com/dbzhang800/QtXlsxWriter/issues?page=1&q=is%3Aissue+is%3Aopen

    Is this the correct place to ask this question? What can I do to fix this?

    Cheers,
    Cedric

    My versions:
    Windows 7 Enterprise SP1
    Qt 5.11.1 mingw53_32
    Xlsx: http://qtxlsx.debao.me/

    My program:
    .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
    

    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;
    }
    
    JonBJ jsulmJ 2 Replies Last reply
    0
    • C cdwijs

      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.

      This problem is not mentioned in the bugtracker:
      https://github.com/dbzhang800/QtXlsxWriter/issues?page=1&q=is%3Aissue+is%3Aopen

      Is this the correct place to ask this question? What can I do to fix this?

      Cheers,
      Cedric

      My versions:
      Windows 7 Enterprise SP1
      Qt 5.11.1 mingw53_32
      Xlsx: http://qtxlsx.debao.me/

      My program:
      .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
      

      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;
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @cdwijs

      Is this the correct place to ask this question? What can I do to fix this?

      http://qtxlsx.debao.me/qtxlsx-module.html looks like a third-party piece of code, developed in 2013/14. It's not any part of Qt itself.

      As such you need to refer to that author about this. Though given the time period I wonder if he is not around.

      Here you are hoping someone is using this XSLX package, and I haven't seen anyone, so best of luck. Someone may be kind enough to look at the lines of code in this Qtxslsx package and see if they can see what's going on, but that's not guaranteed.

      Just a heads-up about how this question stands on this forum.

      P.S.
      To try to workaround from your own code if that's what you'd like to do:

      xlsx.cellAt(0,0)
      

      maybe this Qtxslx has another call to check whether the cell exists which doesn't fall over when it's "empty"? Or maybe you can safely ask it how many rows/columns are valid in the sheet, and look at that before referencing a particular cell?

      1 Reply Last reply
      3
      • C cdwijs

        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.

        This problem is not mentioned in the bugtracker:
        https://github.com/dbzhang800/QtXlsxWriter/issues?page=1&q=is%3Aissue+is%3Aopen

        Is this the correct place to ask this question? What can I do to fix this?

        Cheers,
        Cedric

        My versions:
        Windows 7 Enterprise SP1
        Qt 5.11.1 mingw53_32
        Xlsx: http://qtxlsx.debao.me/

        My program:
        .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
        

        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;
        }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @cdwijs said in qtxlxs segfault when reading an empty excel file using xlsx.cellAt(0,0)->value();:

        variant = xlsx.cellAt(0,0)->value();

        My guess is that

        xlsx.cellAt(0,0)
        

        returns a nullptr and you're then dereferencing it...

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • C Offline
          C Offline
          cdwijs
          wrote on last edited by
          #4

          Thanks jsulm,
          You are correct. It indeed returns 0 when an empty cell is encountered. For future reference, here's the way to first test for a null pointer:

              QXlsx::Cell *cell;
              cell = xlsx.cellAt(2,2); //cell = 0 on empty cell
              if (cell)
              {
                  variant = cell->value();
              }
          

          (Also, 1,1 is the cell at the top-left position, 0,0 is an invalid position)

          Cheers,
          Cedric

          JonBJ 1 Reply Last reply
          1
          • C cdwijs

            Thanks jsulm,
            You are correct. It indeed returns 0 when an empty cell is encountered. For future reference, here's the way to first test for a null pointer:

                QXlsx::Cell *cell;
                cell = xlsx.cellAt(2,2); //cell = 0 on empty cell
                if (cell)
                {
                    variant = cell->value();
                }
            

            (Also, 1,1 is the cell at the top-left position, 0,0 is an invalid position)

            Cheers,
            Cedric

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @cdwijs
            OK, now I see that your Qtxslx does have documentation, which you should look at as you write your code. To use xlsx.cellAt() you should have read http://qtxlsx.debao.me/document.html#cellAt, which tells you:

            Cell * Document::cellAt(int row, int col) const
            Returns the cell at the position pos. If there is no cell at the specified position, the function returns 0.

            I advise you to use the docs for your future work! Also note there are a bunch of examples at http://qtxlsx.debao.me/qtxlsx-examples.html, which can doubtless be useful to your coding.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved