Code review

Tux++ : Maroš Valter, Matěj Plch, Lukáš Toldy

PB173, fall 2014

cppCheck

cppCheck

cppCheck

rats

Knedlíci

Portability

int connectionID;
...
if(sizeof(connectionID) == 4) {
    data[1] = connectionID & 0x000000ff;
    data[2] = (connectionID & 0x0000ff00) >> 8;
    data[3] = (connectionID & 0x00ff0000) >> 16;
    data[4] = (connectionID & 0xff000000) >> 24;
} else return 1;

Portability

#pragma once

Qt

while(true) {
    Sleep(10);
    qApp->processEvents();
}

Qt

return mSocket->waitForBytesWritten(10000);

Security

srand(time(NULL));
//generovani klice
for(int i = sizeof(connectionID); i < dataSize; 
    i++) 
{
    data[i] = rand() % 256;
}

Crypto manager

Threading

while(*end - *start > 0 && 
        *end - *start <= length)
        std::this_thread::sleep_for(sleepTime);

C++11

unsigned char *data = NULL;
bool mLoggedToServer = nullptr;

English

Other issues

mytcpclient.cpp:58: error: taking address of 
    temporary [-fpermissive]
memcpy(mLastReicevedData, 
    &data[ID_LENGHT + RANDOM_BYTES_LENGTH + 4], 
    dataSize);                                                              
memcpy(mLastReicevedData, data.data() + 
    (ID_LENGHT + RANDOM_BYTES_LENGTH + 4), 
    dataSize);

Hrochokobry

Portability

Security

key[i] = rand() % 256;
std::string buffer((const char*)key);

Qt

C++

#define STR_IP( ip ) ip.toString().toStdString()
void Client::SendMessage(std::string line) {..}

C++

Threading

Sapiens

Be consistent

        ++ibuf;
        len--;

Avoid ugly shortcuts

    c2 = *ibuf++;

Format your code

    cout<<clientInfo.id<<"\t: "<<str<<endl;

Avoid unnecessary memory copies

    int  UserIf::message(char* msg, int len)
    {
        char* str = (char*)malloc(len+1);
                strncpy(str, msg, len);
                str[len] = 0;
                cout<<str<<endl;
        free(str);
         return SUCCESS;

    }

Learn how to use pointers

int Client::startSSL( unsigned char* response, 
    int * len)
...
response  = (unsigned char*)malloc(bytes);
    - memory allocated
memcpy(response, buf, bytes); 
    - something copied to the memory
...
return... - response never used and 
            memory is leaked

C++ is rich language

#define FALSE 0
#define TRUE !FALSE

Careful with binary operators

session->e.crypt(data, (len+15)&~0xF, SEND);

Know your tools

unsigned char *data; - some general data
strncpy(str, (char*)data, len);

Never implement your own crypto

    // setting n to a large prime number
    mpz_set_str (n,"9627533057541626274",10);

Follow convetions

Keep things simple

Dynamically allocated memory is important

long clientIP[MAX_CLIENT_LIST];

Thank you!