Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparison of unsigned expressions with 0 #17

Open
lenerd opened this issue Mar 9, 2017 · 0 comments
Open

Comparison of unsigned expressions with 0 #17

lenerd opened this issue Mar 9, 2017 · 0 comments

Comments

@lenerd
Copy link
Collaborator

lenerd commented Mar 9, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant