bloguetronica / cp2130-qt

A C++ class that uses libusb to interface with CP2130 devices. It can be used to configure the OTP ROM of such devices, as well as to control them. The class was made for Qt. If you wish to use a derived non-Qt version, please refer to https://github.com/bloguetronica/cp2130.
0 stars 0 forks source link

In "cp2130.cpp", line 69, the result of operation between QVector::size() and literals is not cast to size_t #31

Closed samuelfmlourenco closed 2 years ago

samuelfmlourenco commented 2 years ago

In "cp2130.cpp", line 69, we have:

size_t length = 2 * descriptor.size() + 2;

Since the variable "descriptor" is of type QString, descriptor.size() returns an int (and not a size_t as it would be expected). The result of the operation should be cast to size_t:

size_t length = static_cast<size_t>(2 * descriptor.size() + 2);

As an alternative, the variable "length" should be an int. This alternative should be evaluated, because it could simplify the implementation of writeDescGeneric(), which the above line is part of. Special attention should be paid to line 80:

controlBufferOut[j] = static_cast<quint8>(descriptor[static_cast<int>((offset + j - 2) / 2)].unicode() >> ((i + j) % 2 == 0 ? 0 : 8));

There is a chance that, if "length" is to be an int, then the innermost static_cast conversion could be omitted.

samuelfmlourenco commented 2 years ago

The most correct approach is to simply cast the result to size_t. Only line 69 should be changed:

size_t length = static_cast<size_t>(2 * descriptor.size() + 2);

samuelfmlourenco commented 2 years ago

Fixed in version 2.2.2.