Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QByteArray toLong false



  • QByteArray a= "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7";
    bool ok;
    long la = a.toLong(&ok, 16);
    qDebug() << la << ok; // 0 false
    

    Python

    a= '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
    int(a, 16)
    //157794750267131502212476817800345498121872783333389747424011531025366277535262539913701806290766479189477533597854989606803194253978660329941980786072432806427833685472618792592200595694346872951301770580765135349259590167490536138082469680638514416594216629258349130257685001248172188325316586707301643237607L
    

  • Moderators

    if it begins with "0", it is assumed to be octal

    So QByteArray thinks the number is octal but finds characters inside... maybe that's the reason for the crash. https://doc.qt.io/qt-5/qbytearray.html#toLong

    In any case, it obviously should not crash, but rather return ok == false. Report it to Qt bugtracker, please. https://bugreports.qt.io/secure/Dashboard.jspa



  • @sierdzio

    I thought the same at first, but the "16" indicates that it should get interpreted as hex (base 16).



  • Well, I don't know what you're expecting... AFAIK long is at most 64 bits, therefore any hex string longer than "0x8000000000000000" won't give you anything meaningful. Apparently python's way is different, but this number is way to large to be handled properly using C++ native integer types.


  • Moderators

    Sure, the conversion should fail. But it should not crash.


  • Moderators

    @sierdzio said in QByteArray toLong crash:

    Sure, the conversion should fail. But it should not crash.

    and it doesn't, at least in my test case:

    int main (int argc, char *argv[])
    {
    
        QApplication app(argc, argv);
    
    
        QByteArray a= "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7";
        bool ok;
        long la = a.toLong(&ok, 16);
        qDebug() << a << ok;
        return  la;
    }
    

    88c5d7c0-3efd-42d6-8d3b-670529dc099d-image.png


  • Moderators

    Hm, interesting. So, @sonichy either you have discovered a crash on some specific compiler or platform, or the crash you see comes from some other part of your code.


  • Lifetime Qt Champion

    @sierdzio said in QByteArray toLong crash:

    or the crash you see comes from some other part of your code.

    I'm pretty sure :)

    Btw, does not crash on Linux with Qt 5.14.1

    Regards



  • @sonichy said in QByteArray toLong crash:

    or the crash you see

    What crash? Please post the actual stack trace



  • @JohanSolo said in QByteArray toLong crash:

    Apparently python's way is different

    Seems like Python has an unlimited long int precision,

    Plain integers (also just called integers) are implemented using long in C, which gives them at least 32 bits of precision. Long integers have unlimited precision. Floating point numbers are implemented using double in C.

    (from: https://docs.python.org/2.4/lib/typesnumeric.html)

    while C++ doesn't have it (of course). Usually 16b, 32b, 64b for short, int, long.

    @sonichy
    So you can't process your large byte array as single value in C++.

    @J-Hilk
    Haven't tested myself, but does your long really equal the value of that byte array, or is qDebug() just printing an array of hex chars?
    Nevermind, I'm an idiot :D You do print that QByteArray , not the actual long int... :)

    Edit: Interesting, that you ( @J-Hilk ) return la and the value is 0. The null could be the reason for the crash, @sonichy is facing. Depends on where and how that long is used later in the code.



  • @Pl45m4 said in QByteArray toLong crash:

    Edit: Interesting, that you ( @J-Hilk ) return la and the value is 0. The null could be the reason for the crash, @sonichy is facing. Depends on where and how that long is used later in the code.

    ? Even if the value is 0 (I don't know whether it is or not), it's a number, zero, not a pointer, null, and won't (certinaly shouldn't) be used as such later. Why do you feel that would influence a crash?


  • Qt Champions 2019

    @JonB said in QByteArray toLong crash:

    Why do you feel that would influence a crash?

    val = ba.toLong(...); // returns 0
    newVal = 42 / val

    :)



  • @Christian-Ehrlicher
    Don't be silly ;-) The input stream is presumably some numbers, the fact that one comes out as 0 shouldn't lead it to be treated as a null pointer is all I'm saying :)



  • @JonB

    As I've said, I dont know what OP is trying to do with his long later ( sounds wrong... :-D ).
    If it's somehow important for the value to be higher than 0, this would definitely crash then... Just my thoughts.
    And I didnt mentioned any pointer :)


Log in to reply