From eec31edb1c6fe5d0ffdc79f6b38a89f6a4a9b20d Mon Sep 17 00:00:00 2001 From: AZ Huang Date: Fri, 29 Nov 2013 18:26:09 +0800 Subject: [PATCH 1/2] Fix return code inconsistency. --- testing/nTox.c | 6 +++--- toxcore/Messenger.c | 44 ++++++++++++++++++++++---------------------- toxcore/tox.c | 12 ++++++------ toxcore/tox.h | 12 ++++++------ 4 files changed, 37 insertions(+), 37 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index 2dd8b874..6e333abf 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -131,8 +131,8 @@ void send_filesenders(Tox *m) continue; while (1) { - if (!tox_file_senddata(m, file_senders[i].friendnum, file_senders[i].filenumber, file_senders[i].nextpiece, - file_senders[i].piecelength)) + if (tox_file_senddata(m, file_senders[i].friendnum, file_senders[i].filenumber, file_senders[i].nextpiece, + file_senders[i].piecelength) == -1) break; file_senders[i].piecelength = fread(file_senders[i].nextpiece, 1, tox_filedata_size(m, file_senders[i].friendnum), @@ -1182,7 +1182,7 @@ void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t sprintf(msg, "[t] %u is sending us: %s of size %llu", friendnumber, filename, (long long unsigned int)filesize); new_lines(msg); - if (tox_file_sendcontrol(m, friendnumber, 1, filenumber, 0, 0, 0)) { + if (tox_file_sendcontrol(m, friendnumber, 1, filenumber, 0, 0, 0) == 0) { sprintf(msg, "Accepted file transfer. (saving file as: %u.%u.bin)", friendnumber, filenumber); new_lines(msg); } else diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 5e9758dd..a3295170 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1185,28 +1185,28 @@ int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *f /* Send a file control request. * send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file. * - * return 1 on success - * return 0 on failure + * return 0 on success + * return -1 on failure */ int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, uint8_t *data, uint16_t length) { if (length > MAX_DATA_SIZE - 3) - return 0; + return -1; if (friend_not_valid(m, friendnumber)) - return 0; + return -1; if (send_receive == 1) { if (m->friendlist[friendnumber].file_receiving[filenumber].status == FILESTATUS_NONE) - return 0; + return -1; } else { if (m->friendlist[friendnumber].file_sending[filenumber].status == FILESTATUS_NONE) - return 0; + return -1; } if (send_receive > 1) - return 0; + return -1; uint8_t packet[MAX_DATA_SIZE]; packet[0] = send_receive; @@ -1216,7 +1216,7 @@ int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t f if (message_id == FILECONTROL_RESUME_BROKEN) { if (length != sizeof(uint64_t)) - return 0; + return -1; uint8_t remaining[sizeof(uint64_t)]; memcpy(remaining, data, sizeof(uint64_t)); @@ -1264,32 +1264,32 @@ int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t f break; } - return 1; - } else { return 0; + } else { + return -1; } } #define MIN_SLOTS_FREE 4 /* Send file data. * - * return 1 on success - * return 0 on failure + * return 0 on success + * return -1 on failure */ int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) { if (length > MAX_DATA_SIZE - 1) - return 0; + return -1; if (friend_not_valid(m, friendnumber)) - return 0; + return -1; if (m->friendlist[friendnumber].file_sending[filenumber].status != FILESTATUS_TRANSFERRING) - return 0; + return -1; /* Prevent file sending from filling up the entire buffer preventing messages from being sent. */ if (crypto_num_free_sendqueue_slots(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id) < MIN_SLOTS_FREE) - return 0; + return -1; uint8_t packet[MAX_DATA_SIZE]; packet[0] = filenumber; @@ -1297,10 +1297,10 @@ int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, if (write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_DATA, packet, length + 1)) { m->friendlist[friendnumber].file_sending[filenumber].transferred += length; - return 1; + return 0; } - return 0; + return -1; } @@ -1309,22 +1309,22 @@ int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, * send_receive is 0 if we want the sending files, 1 if we want the receiving. * * return number of bytes remaining to be sent/received on success - * return 0 on failure + * return -1 on failure */ uint64_t file_dataremaining(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t send_receive) { if (friend_not_valid(m, friendnumber)) - return 0; + return -1; if (send_receive == 0) { if (m->friendlist[friendnumber].file_sending[filenumber].status == FILESTATUS_NONE) - return 0; + return -1; return m->friendlist[friendnumber].file_sending[filenumber].size - m->friendlist[friendnumber].file_sending[filenumber].transferred; } else { if (m->friendlist[friendnumber].file_receiving[filenumber].status == FILESTATUS_NONE) - return 0; + return -1; return m->friendlist[friendnumber].file_receiving[filenumber].size - m->friendlist[friendnumber].file_receiving[filenumber].transferred; diff --git a/toxcore/tox.c b/toxcore/tox.c index d5bc1c35..02137b3c 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -585,8 +585,8 @@ int tox_new_filesender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t *f /* Send a file control request. * send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file. * - * return 1 on success - * return 0 on failure + * return 0 on success + * return -1 on failure */ int tox_file_sendcontrol(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, uint8_t *data, uint16_t length) @@ -596,8 +596,8 @@ int tox_file_sendcontrol(Tox *tox, int friendnumber, uint8_t send_receive, uint8 } /* Send file data. * - * return 1 on success - * return 0 on failure + * return 0 on success + * return -1 on failure */ int tox_file_senddata(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) { @@ -608,7 +608,7 @@ int tox_file_senddata(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *d /* Returns the recommended/maximum size of the filedata you send with tox_file_senddata() * * return size on success - * return 0 on failure (currently will never return 0) + * return -1 on failure (currently will never return -1) */ int tox_filedata_size(Tox *tox, int friendnumber) { @@ -620,7 +620,7 @@ int tox_filedata_size(Tox *tox, int friendnumber) * send_receive is 0 if we want the sending files, 1 if we want the receiving. * * return number of bytes remaining to be sent/received on success - * return 0 on failure + * return -1 on failure */ uint64_t tox_file_dataremaining(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive) { diff --git a/toxcore/tox.h b/toxcore/tox.h index 469a1f78..735d991a 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -519,23 +519,23 @@ int tox_new_filesender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t *f * send_receive is 0 if we want the control packet to target a file we are currently sending, * 1 if it targets a file we are currently receiving. * - * return 1 on success - * return 0 on failure + * return 0 on success + * return -1 on failure */ int tox_file_sendcontrol(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, uint8_t *data, uint16_t length); /* Send file data. * - * return 1 on success - * return 0 on failure + * return 0 on success + * return -1 on failure */ int tox_file_senddata(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length); /* Returns the recommended/maximum size of the filedata you send with tox_file_senddata() * * return size on success - * return 0 on failure (currently will never return 0) + * return -1 on failure (currently will never return -1) */ int tox_filedata_size(Tox *tox, int friendnumber); @@ -544,7 +544,7 @@ int tox_filedata_size(Tox *tox, int friendnumber); * send_receive is 0 if we want the sending files, 1 if we want the receiving. * * return number of bytes remaining to be sent/received on success - * return 0 on failure + * return -1 on failure */ uint64_t tox_file_dataremaining(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive); From 1df2d5880db375b6a2d5259bb8ff2560c9f1007d Mon Sep 17 00:00:00 2001 From: AZ Huang Date: Fri, 29 Nov 2013 22:26:29 +0800 Subject: [PATCH 2/2] Should return 0 for tox_file_dataremaining since it returns unsigned --- toxcore/Messenger.c | 6 +++--- toxcore/tox.c | 2 +- toxcore/tox.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index a3295170..954b31dd 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1314,17 +1314,17 @@ int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint64_t file_dataremaining(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t send_receive) { if (friend_not_valid(m, friendnumber)) - return -1; + return 0; if (send_receive == 0) { if (m->friendlist[friendnumber].file_sending[filenumber].status == FILESTATUS_NONE) - return -1; + return 0; return m->friendlist[friendnumber].file_sending[filenumber].size - m->friendlist[friendnumber].file_sending[filenumber].transferred; } else { if (m->friendlist[friendnumber].file_receiving[filenumber].status == FILESTATUS_NONE) - return -1; + return 0; return m->friendlist[friendnumber].file_receiving[filenumber].size - m->friendlist[friendnumber].file_receiving[filenumber].transferred; diff --git a/toxcore/tox.c b/toxcore/tox.c index 02137b3c..f4e65ec8 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -620,7 +620,7 @@ int tox_filedata_size(Tox *tox, int friendnumber) * send_receive is 0 if we want the sending files, 1 if we want the receiving. * * return number of bytes remaining to be sent/received on success - * return -1 on failure + * return 0 on failure */ uint64_t tox_file_dataremaining(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive) { diff --git a/toxcore/tox.h b/toxcore/tox.h index 735d991a..ea373488 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -544,7 +544,7 @@ int tox_filedata_size(Tox *tox, int friendnumber); * send_receive is 0 if we want the sending files, 1 if we want the receiving. * * return number of bytes remaining to be sent/received on success - * return -1 on failure + * return 0 on failure */ uint64_t tox_file_dataremaining(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive);