Solved QByteArray not initializing
-
@mzimmers: Your problem with Mac8 is most likely that the array has an unequal number of chars...
PS: I already said in your other thread that you can use QByteArray::fromHex().
-
@aha_1980 not sure what you mean by "an unequal number of chars".
I saw your other response, but didn't understand it. I'll take another look.
-
@mzimmers said in QByteArray not initializing:
@aha_1980 not sure what you mean by "an unequal number of chars".
strlen(Mac8) == 65
Your loop is iterating with steps of two. So you're reading over the end of Mac8 with sscanf().
I'm not sure what QByteArray::fromHex() will do with this invalid input, but most likely it will not crash. (If it does, we create a bugreport).
-
@aha_1980 this crash is occurring on the second iteration through the loop, so I don't think it's an array/string overrun.
-
Oddly enough i dont crash here
it just converts to 32 values each time. -
@mzimmers
Hi
Can you try
// QCoreApplication a(argc, argv);
// a.exec();
and just return 0;You are not sending it a close signal so i think crash is due to killing it with debugger.
At least i cannot crash it without running the coreapp and exec().
-
@mrjj yeah, I'm pretty sure my loops are OK. I honestly don't know what's going on -- maybe some weird bug in the constructor.
I removed the core app stuff...same results.
I think I'll implement aha_1980's suggestion. I took another look, and it's a lot simpler than this. I'll report back on how I do with it.
-
@mzimmers
Ok. i checked the loop and its 64 in length and result is 32 in bytearray.
no crashes at any time.
Very odd then.
Im on 64 bit. -
I'm on Windows 10, using MinGW, FWIW. Just tried null terminating the string myself, and still bombs in the loop (though aha's suggestion works).
-
@mzimmers
Hmm visual studio here.
Maybe its related to mingw.
I will try tomorrow at work with mingw and see.
I dont like when crashes are not reproducible. -
@mzimmers said in QByteArray not initializing:
@mrjj here's the code in its entirety:
uint8_t c; sscanf(p, "%2x", (int *) &c);
c
holds a 8-bitint
, right? Firstly that's not right for(int *) &c
, and secondly look atsscanf("%2x"
, it will expect the address of a wholeint
(32-bit? 64-bit?) to store its result. So you're overwriting the local stack space, hence possible crashing? Or,uint8_t
is allocated on an odd address boundary, which won't be good.Make
c
be a properint
, or fix yoursscanf
.Your understanding of
QByteArray
is fine, but not ofscanf
:-)Also, if you use
QByteArray::fromHex()
, you won't need to usesscanf
:) -
@JNBarchan the pointer should be OK, as far as I can see. Your point about the sscanf() overwriting the output buffer may be valid, though it's really odd that the first two loops worked fine.
-
@JNBarchan
Thats a really good observation and might explain why
no crashing here 64bit. Completely overlooked it :)
-
@mzimmers said in QByteArray not initializing:
@JNBarchan the pointer should be OK
What pointer, and what do you mean by "OK"?
P.S. I can explain why it goes through sometimes and fails others, but it's going to mean quite a bit of typing, do you really want to know?
-
@mrjj
If thatsscanf
supports%hhx
that should work. -
@JNBarchan in this line of code:
sscanf(p, "%2x", (int *) &c);
the address formed by the cast should be fine, as far as I can tell.
I agree the problem is in the sscanf. This line of code:
sscanf(p, "%hhx", &c);
compiles but doesn't work right (c keeps getting 0xff). I'm going to go with aha_1980's approach and call it a day.
Thanks to everyone who looked at this.
-
@mzimmers said in QByteArray not initializing:
@JNBarchan in this line of code:
sscanf(p, "%2x", (int *) &c);
the address formed by the cast should be fine, as far as I can tell.
The address is indeed a valid address, but it points to an area reserved to hold one byte and you're storing 4 or 8 bytes there, which is very naughty! Hope you understand.
-
@JNBarchan said in QByteArray not initializing:
which is very naughty!
Buffer overflows - the pinnacle of C programming ... ;)
-
@kshegunov I expected the "%2x" in the sscanf to restrict output to one byte (2 hex characters), but I guess it doesn't work that way.
-
Not really, no. There's no compile-time type checking with variadic functions (which
scanf
and related are). It will expect you to give it a proper integer to write to. But really your error is the cast itself. You can't cast pointers like this. You have achar
on the stack and you cast its address to aint *
. From there on the compiler will consider that this pointer is referencing an integer, meaning you have "hijacked" 3 bytes from the nearby variables in the stack. If you modify your code like this:uint8_t c, c2, c3, c4; // ... sscanf(p, "%2x", (int *) &c); // ...
You'd discover that all those variables -
c2
,c3
andc4
are modified alongside your originalc
becausesscanf
overruns the given buffer and writes outside of it.