encryptogroup / ABY

ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
GNU Lesser General Public License v3.0
463 stars 132 forks source link

Comparison of unsigned expressions with 0 #17

Open lenerd opened 7 years ago

lenerd commented 7 years ago
src/abycore/util/socket.h:240:12: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
                        if (ret < 0) {
                            ~~~ ^ ~ 

This is in the method uint64_t Receive(void* pBuf, uint64_t nLen, int nFlags = 0).

The variable ret is declared as unsigned. It used to store the return value of recv.

uint64_t ret = 0;
[...]
ret = recv(m_hSock, p, n, 0);

recv returns a signed value (ssize_t in POSIX, int in Winsock), where a negative value represents an error.

Since it is assigned to an unsigned variable, the error handling will never be executed:

        if (ret < 0) {
            if ( errno == EAGAIN) {
                cerr << "socket recv eror: EAGAIN" << endl;
                SleepMiliSec(200);
                continue;
            } else {
                cerr << "socket recv error: " << errno << endl;
                perror("Socket error "); 
                return ret;
            }   
        } else if (ret == 0) {

If an error occurs, the negative return value will be converted to a large positive number. That will probably result in undesired behavior.

src/abycore/util/crypto/crypto.cpp:447:14: warning: comparison of unsigned express ion < 0 is always false [-Wtautological-compare]
                if (result < 0) {
                    ~~~~~~ ^ ~

The same thing happens here, read returns a ssize_t.

void gen_secure_random(uint8_t* dest, uint32_t nbytes) {
    int32_t randomData = open("/dev/random", O_RDONLY);
    uint32_t bytectr = 0;
    while (bytectr < nbytes) {
        uint32_t result = read(randomData, dest + bytectr, nbytes - bytectr);
        if (result < 0) {
            cerr << "Unable to read from /dev/random, exiting" << endl;
            exit(0);
        }
        bytectr += result;
    }
    close(randomData);
}

src/examples/psi_scs/common/sort_compare_shuffle.cpp:320:35: warning: comparison of unsigned expression >= 0 is always true [-Wtautological-compare]
                        for (k = 0; k < i && j - i - k >= 0; k++) {
                                             ~~~~~~~~~ ^  ~

Here is another issue.