Skip to content

Commit

Permalink
Add some checking to the code through asserts. Fixes some "dead incre…
Browse files Browse the repository at this point in the history
…ment" found by the static analyzer.
  • Loading branch information
Youx committed Oct 13, 2009
1 parent 51d8bb9 commit faf846f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 deletions.
3 changes: 3 additions & 0 deletions acknowledge_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ void send_acknowledge(struct player *pl)
*(uint32_t *)ptr = pl->public_id; ptr+=4;
*(uint32_t *)ptr = pl->f1_s_counter; ptr+=4;

/* check we filled the whole packet */
assert((ptr - data) == data_size);

err = sendto(pl->in_chan->in_server->socket_desc, data, data_size, 0, (struct sockaddr *)pl->cli_addr, pl->cli_len);
if (err == -1) {
logger(LOG_ERR, "send_acknowledge, sending data failed : %s.", strerror(errno));
Expand Down
5 changes: 4 additions & 1 deletion audio_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>

/** The size of the raw audio block (in bytes) */
size_t codec_audio_size[13] = {153, 51, 165, 132, 0, 27, 50, 75, 100, 138, 188, 228, 308};
Expand Down Expand Up @@ -105,7 +106,9 @@ int audio_received(char *in, size_t len, struct server *s)
*(uint32_t *)ptr = sender->public_id; ptr += 4; /* ID of sender */
*(uint16_t *)ptr = *(uint16_t *)(in + 12); ptr += 2; /* conversation counter */
memcpy(ptr, in + 16, audio_block_size); ptr += audio_block_size;

/* assert we filled the whole packet */
assert((ptr - data) == data_size);

ar_each(struct player *, tmp_pl, iter, ch_in->players)
if (tmp_pl != sender && !ar_has(tmp_pl->muted, sender)) {
*(uint32_t *)(data + 4) = tmp_pl->private_id;
Expand Down
2 changes: 1 addition & 1 deletion ban.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,5 @@ int ban_to_data(struct ban *b, char *dest)
*(uint16_t *)ptr = b->duration; ptr += 2; /* duration in minutes */
strcpy(ptr, b->reason); ptr += strlen(b->reason) + 1; /* reason */

return ban_to_data_size(b);
return ptr - dest;
}
19 changes: 16 additions & 3 deletions connection_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@
static void server_accept_connection(struct player *pl)
{
char *data, *ptr;
int data_size = 436;
struct server *s = pl->in_chan->in_server;

data = (char *)calloc(436, sizeof(char));
data = (char *)calloc(data_size, sizeof(char));
if (data == NULL) {
logger(LOG_WARN, "server_accept_connection : calloc failed : %s.", strerror(errno));
return;
Expand Down Expand Up @@ -79,6 +80,9 @@ static void server_accept_connection(struct player *pl)
*ptr = MIN(255, strlen(s->welcome_msg)); ptr += 1; /* Length of welcome message */
strncpy(ptr, s->welcome_msg, *(ptr - 1)); ptr += 255; /* Welcome message */

/* check we filled the whole packet */
assert((ptr - data) == data_size);

/* Add CRC */
packet_add_crc(data, 436, 16);
/* Send packet */
Expand All @@ -97,8 +101,9 @@ static void server_accept_connection(struct player *pl)
static void server_refuse_connection_ban(struct sockaddr_in *cli_addr, int cli_len, struct server *s)
{
char *data, *ptr;
int data_size = 436;

data = (char *)calloc(436, sizeof(char));
data = (char *)calloc(data_size, sizeof(char));
if (data == NULL) {
logger(LOG_WARN, "server_refuse_connection : calloc failed : %s.", strerror(errno));
return;
Expand Down Expand Up @@ -127,6 +132,9 @@ static void server_refuse_connection_ban(struct sockaddr_in *cli_addr, int cli_l
/* *ptr = 26;*/ ptr += 1; /* Length of welcome message */
/* memcpy(ptr, "Bienvenue sur mon serveur.", 26);*/ ptr += 255; /* Welcome message */

/* check we filled the whole packet */
assert((ptr - data) == data_size);

/* Add CRC */
packet_add_crc(data, 436, 16);
/* Send packet */
Expand Down Expand Up @@ -213,8 +221,9 @@ void handle_player_connect(char *data, unsigned int len, struct sockaddr_in *cli
static void s_resp_keepalive(struct player *pl, uint32_t ka_id)
{
char *data, *ptr;
int data_size = 24;

data = (char *)calloc(24, sizeof(char));
data = (char *)calloc(data_size, sizeof(char));
if (data == NULL) {
logger(LOG_WARN, "s_resp_keepalive : calloc failed : %s.", strerror(errno));
return;
Expand All @@ -227,6 +236,10 @@ static void s_resp_keepalive(struct player *pl, uint32_t ka_id)
*(uint32_t *)ptr = pl->f4_s_counter; ptr += 4; /* Packet counter */
/* Checksum initialize at the end */ ptr += 4;
*(uint32_t *)ptr = ka_id; ptr += 4; /* ID of the keepalive to confirm */

/* check we filled the whole packet */
assert((ptr - data) == data_size);

/* Add CRC */
packet_add_crc(data, 24, 16);

Expand Down
1 change: 0 additions & 1 deletion database/db_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ struct server **db_create_servers(struct config *c)
}

if (res) {
i = 0;
for (i = 0 ; dbi_result_next_row(res) ; i++) {
ss[i] = new_server();
ss[i]->conf = c;
Expand Down
1 change: 1 addition & 0 deletions main_serv.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ int main(int argc, char **argv)
print_help(argv[0]);
terminate = 1;
helpshown = 1;
break;
case 'v':
print_version();
terminate = 1;
Expand Down
4 changes: 4 additions & 0 deletions player.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <stdio.h>
#include <sys/socket.h>
#include <errno.h>
#include <assert.h>


/**
Expand Down Expand Up @@ -141,6 +142,9 @@ struct player *new_player_from_data(char *data, int len, struct sockaddr_in *cli
password = strndup(ptr, tmp_size); ptr += 29; /* password */
tmp_size = MIN(29, *ptr); ptr += 1; /* size of nickname */
nickname = strndup(ptr, tmp_size); ptr += 29; /* nickname */

/* check we filled the whole data */
assert(ptr - data == len);

/* Initialize player */
pl = new_player(nickname, login, machine);
Expand Down

0 comments on commit faf846f

Please sign in to comment.