created tool for automatically finding IN()s without matching

OUT()s or where return() is used instead of RETURN().
Added OUT() to end of all functions using IN() that lacked it to
make it easier to statically analyse this invariant.
Fixed several return instead of RETURNs detected through use of
this tool. #49
This commit is contained in:
gardners 2013-02-17 04:17:24 +10:30 committed by Jeremy Lakeman
parent 2bbdd523f0
commit 2252fdcaa7
28 changed files with 143 additions and 10 deletions

61
check-in-out-return.c Normal file
View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <string.h>
int line_count=0;
char *filename=NULL;
int dofile(char *file)
{
FILE *f=fopen(file,"r");
char line[1024];
if (filename) free(filename);
filename=strdup(file);
line_count=0;
int depth=0;
int inoutActive=0;
int savedActive=0;
int commentMode=0;
int quoteMode=0;
line[0]=0; fgets(line,1024,f);
while(line[0]) {
line_count++;
int i;
for(i=0;i<strlen(line);i++) {
if (!strncmp(&line[i],"//",2)) break;
if (!strncmp(&line[i],"/*",2)) commentMode=1;
if (!strncmp(&line[i],"*/",2)) commentMode=0;
if (!strncmp(&line[i],"\"",1)) quoteMode^=1;
if (commentMode||quoteMode) continue;
if (line[i]=='{') depth++;
if (line[i]=='}') {
if (depth>0) depth--;
if (!depth) {
if (inoutActive)
fprintf(stderr,"%s:%d:Leaving function without OUT() or RETURN?\n",
filename,line_count);
inoutActive=0;
savedActive=0;
} else inoutActive=savedActive;
}
if (!strncmp(&line[i],"IN()",4)) { inoutActive=1; savedActive=1; }
if (!strncmp(&line[i],"OUT()",5)) inoutActive=0;
if (inoutActive&&
(!strncmp(&line[i],"return",6)))
{
fprintf(stderr,
"%s:%d:Called return instead of RETURN in function with IN()\n",
filename,line_count);
}
}
line[0]=0; fgets(line,1024,f);
}
}
int main(int argc,char **argv)
{
int i;
for(i=1;i<argc;i++) dofile(argv[i]);
}

1
cli.c
View File

@ -204,6 +204,7 @@ int cli_invoke(const struct cli_parsed *parsed, void *context)
IN();
int ret = parsed->command->function(parsed, context);
RETURN(ret);
OUT();
}
int _cli_arg(struct __sourceloc __whence, const struct cli_parsed *parsed, char *label, const char **dst, int (*validator)(const char *arg), char *defaultvalue)

View File

@ -712,7 +712,7 @@ int app_server_start(const struct cli_parsed *parsed, void *context)
RETURN(WHY("Must supply <exec path> argument when invoked via JNI"));
#endif
if ((tmp = malloc(PATH_MAX)) == NULL)
return WHY("Out of memory");
RETURN(WHY("Out of memory"));
if (get_self_executable_path(tmp, PATH_MAX) == -1)
RETURN(WHY("unable to determine own executable name"));
execpath = tmp;
@ -828,6 +828,7 @@ int app_server_start(const struct cli_parsed *parsed, void *context)
}
}
RETURN(ret);
OUT();
}
int app_server_stop(const struct cli_parsed *parsed, void *context)

View File

@ -212,6 +212,7 @@ ATOM(char, rhizome_ads, 0, cf_opt_char_boolean,, "")
ATOM(char, manifests, 0, cf_opt_char_boolean,, "")
ATOM(char, vomp, 0, cf_opt_char_boolean,, "")
ATOM(char, trace, 0, cf_opt_char_boolean,, "")
ATOM(char, profiling, 0, cf_opt_char_boolean,, "")
END_STRUCT
STRUCT(log)

View File

@ -32,8 +32,8 @@ int crypto_verify_signature(unsigned char *sas_key,
WHY("Signature verification failed");
RETURN(-1);
}
RETURN(0);
OUT();
}
// verify the signature at the end of a message, on return message_len will be reduced by the length of the signature.
@ -75,7 +75,8 @@ int crypto_create_signature(unsigned char *key,
bcopy(sig, signature, SIGNATURE_BYTES);
*sig_length=SIGNATURE_BYTES;
RETURN(0);
OUT();
return 0;
}
// sign the hash of a message, adding the signature to the end of the message buffer.

View File

@ -311,4 +311,5 @@ int fd_poll()
}
}
RETURN(1);
OUT();
}

View File

@ -778,7 +778,7 @@ int keyring_enter_pin(keyring_file *k, const char *pin)
/* Tell the caller how many identities we found */
RETURN(identitiesFound);
OUT();
}
/* Create a new identity in the specified context (which supplies the keyring pin)
@ -1146,6 +1146,7 @@ unsigned char *keyring_find_sas_private(keyring_file *k,unsigned char *sid,
}
RETURNNULL(WHYNULL("Identity lacks SAS"));
OUT();
}
static int keyring_store_sas(overlay_mdp_frame *req){
@ -1341,6 +1342,7 @@ keyring_file *keyring_open_instance()
if ((k = keyring_open(keyringFile)) == NULL)
RETURN(NULL);
RETURN(k);
OUT();
}
keyring_file *keyring_open_instance_cli(const struct cli_parsed *parsed)
@ -1360,6 +1362,7 @@ keyring_file *keyring_open_instance_cli(const struct cli_parsed *parsed)
if (strn_str_cmp(parsed->labelv[i].label, parsed->labelv[i].len, "--entry-pin") == 0)
keyring_enter_pin(k, parsed->labelv[i].text);
RETURN(k);
OUT();
}
/* If no identities, create an initial identity with a phone number.
@ -1454,4 +1457,5 @@ unsigned char *keyring_get_nm_bytes(unsigned char *known_sid, unsigned char *unk
->keypairs[kp]->private_key);
RETURN(nm_cache[i].nm_bytes);
OUT();
}

View File

@ -90,7 +90,7 @@ int overlayServerMode()
HLR for DNA lookups, even in non-overlay mode. */
keyring = keyring_open_instance();
if (!keyring)
return WHY("Could not open serval keyring file.");
RETURN(WHY("Could not open serval keyring file."));
keyring_enter_pin(keyring, "");
/* put initial identity in if we don't have any visible */
keyring_seed(keyring);
@ -163,4 +163,5 @@ schedule(&_sched_##X); }
while(fd_poll());
RETURN(0);
OUT();
}

View File

@ -379,6 +379,7 @@ int send_please_explain(struct decode_context *context, struct subscriber *sourc
RETURN(0);
op_free(frame);
RETURN(-1);
OUT();
}
// process an incoming request for explanation of subscriber abbreviations

View File

@ -225,4 +225,5 @@ int overlay_route_saw_advertisements(int i, struct overlay_frame *f, struct deco
// restore the previous subscriber id for parsing the next header
context->previous=previous;
RETURN(0);
OUT();
}

View File

@ -571,6 +571,7 @@ static void interface_read_file(struct overlay_interface *interface)
ssize_t nread = read(interface->alarm.poll.fd, &packet, sizeof packet);
if (nread == -1){
WHY_perror("read");
OUT();
return;
}
@ -613,6 +614,7 @@ static void interface_read_file(struct overlay_interface *interface)
interface->alarm.deadline = interface->alarm.alarm + 10000;
}
}
OUT();
}
static void interface_read_stream(struct overlay_interface *interface){

View File

@ -179,6 +179,7 @@ int resolve_name(const char *name, struct in_addr *addr){
freeaddrinfo(addresses);
RETURN(ret);
OUT();
}
// load a unicast address from configuration
@ -242,6 +243,7 @@ overlay_mdp_service_probe(overlay_mdp_frame *mdp)
peer->address.sin_port = probe.addr.sin_port;
set_reachable(peer, REACHABLE_UNICAST | (peer->reachable & REACHABLE_DIRECT));
RETURN(0);
OUT();
}
int overlay_send_probe(struct subscriber *peer, struct sockaddr_in addr, overlay_interface *interface, int queue){

View File

@ -360,6 +360,7 @@ int overlay_mdp_decrypt(struct overlay_frame *f, overlay_mdp_frame *mdp)
}
}
RETURN(WHY("Failed to decode mdp payload"));
OUT();
}
int overlay_saw_mdp_containing_frame(struct overlay_frame *f, time_ms_t now)
@ -391,6 +392,7 @@ int overlay_saw_mdp_containing_frame(struct overlay_frame *f, time_ms_t now)
/* and do something with it! */
RETURN(overlay_saw_mdp_frame(f, &mdp,now));
OUT();
}
int overlay_mdp_swap_src_dst(overlay_mdp_frame *mdp)
@ -474,6 +476,7 @@ static int overlay_saw_mdp_frame(struct overlay_frame *frame, overlay_mdp_frame
}
RETURN(0);
OUT();
}
int overlay_mdp_dnalookup_reply(const sockaddr_mdp *dstaddr, const unsigned char *resolved_sid, const char *uri, const char *did, const char *name)
@ -777,6 +780,7 @@ int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int userGeneratedFrameP,
if (overlay_payload_enqueue(frame))
op_free(frame);
RETURN(0);
OUT();
}
static int search_subscribers(struct subscriber *subscriber, void *context){

View File

@ -137,6 +137,7 @@ int overlay_mdp_service_rhizomerequest(overlay_mdp_frame *mdp)
rhizome_database_blob_close(blob); blob=NULL;
RETURN(-1);
OUT();
}
int overlay_mdp_service_rhizomeresponse(overlay_mdp_frame *mdp)
@ -174,8 +175,8 @@ int overlay_mdp_service_rhizomeresponse(overlay_mdp_frame *mdp)
break;
}
RETURN(-1);
OUT();
}
int overlay_mdp_service_dnalookup(overlay_mdp_frame *mdp)

View File

@ -106,6 +106,7 @@ int process_incoming_frame(time_ms_t now, struct overlay_interface *interface, s
RETURN(WHYF("Support for f->type=0x%x not implemented",f->type));
}
RETURN(0);
OUT();
}
// duplicate the frame and queue it
@ -136,6 +137,7 @@ int overlay_forward_payload(struct overlay_frame *f){
}
RETURN(0);
OUT();
}
// Parse the mdp envelope header
@ -236,6 +238,7 @@ int parseMdpPacketHeader(struct decode_context *context, struct overlay_frame *f
forward=process=0;
}
RETURN(forward|process);
OUT();
}
int parseEnvelopeHeader(struct decode_context *context, struct overlay_interface *interface,
@ -307,6 +310,7 @@ int parseEnvelopeHeader(struct decode_context *context, struct overlay_interface
context->addr=interface->broadcast_address;
}
RETURN(0);
OUT();
}
int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, size_t len,
@ -471,4 +475,5 @@ end:
ob_free(b);
RETURN(ret);
OUT();
}

View File

@ -541,6 +541,7 @@ overlay_fill_send_packet(struct outgoing_packet *packet, time_ms_t now) {
RETURN(1);
}
RETURN(0);
OUT();
}
// when the queue timer elapses, send a packet

View File

@ -627,6 +627,7 @@ int overlay_route_recalc_neighbour_metrics(struct overlay_neighbour *n, time_ms_
overlay_route_recalc_node_metrics(n->node, now);
RETURN(0);
OUT();
}
/*
@ -667,6 +668,7 @@ int overlay_route_saw_selfannounce_ack(struct overlay_frame *f,long long now)
overlay_route_node_can_hear_me(f->source,iface,s1,s2,now);
RETURN(0);
OUT();
}
/* if to and via are the same, then this is evidence that we can get to the
@ -750,6 +752,7 @@ int overlay_route_record_link(time_ms_t now, struct subscriber *to,
overlay_route_dump();
RETURN(0);
OUT();
}
int node_dump(struct subscriber *subscriber, void *context){

View File

@ -198,6 +198,10 @@ void dump_stack()
int fd_func_enter(struct __sourceloc __whence, struct call_stats *this_call)
{
if (config.debug.profiling)
DEBUGF("%s called from %s() %s:%d",
__FUNCTION__,__whence.function,__whence.file,__whence.line);
this_call->enter_time=gettime_ms();
this_call->child_time=0;
this_call->prev = current_call;
@ -211,6 +215,10 @@ int fd_func_exit(struct __sourceloc __whence, struct call_stats *this_call)
// probably points to somewhere on the stack (see the IN() macro) that has since been overwritten,
// so no sense in trying to print its contents in a diagnostic message; that would just cause
// a SEGV.
if (config.debug.profiling)
DEBUGF("%s called from %s() %s:%d",
__FUNCTION__,__whence.function,__whence.file,__whence.line);
if (current_call != this_call)
FATAL("performance timing stack trace corrupted");

View File

@ -313,6 +313,7 @@ int rhizome_read_manifest_file(rhizome_manifest *m, const char *filename, int bu
}
RETURN(0);
OUT();
}
int rhizome_hash_file(rhizome_manifest *m,const char *filename,char *hash_out)
@ -686,6 +687,7 @@ int rhizome_manifest_finalise(rhizome_manifest *m, rhizome_manifest **mout)
ret=rhizome_add_manifest(m, 255 /* TTL */);
RETURN(ret);
OUT();
}
int rhizome_fill_manifest(rhizome_manifest *m, const char *filepath, const sid_t *authorSid, rhizome_bk_t *bsk){

View File

@ -58,7 +58,7 @@ int rhizome_bk_xor_stream(
int xor_stream_byte_count)
{
IN();
if (rs_len<1||rs_len>65536) return WHY("rs_len invalid");
if (rs_len<1||rs_len>65536) RETURN(WHY("rs_len invalid"));
if (xor_stream_byte_count<1||xor_stream_byte_count>crypto_hash_sha512_BYTES)
RETURN(WHY("xor_stream_byte_count invalid"));
@ -70,7 +70,8 @@ int rhizome_bk_xor_stream(
crypto_hash_sha512(hash,buffer,combined_len);
bcopy(hash,xor_stream,xor_stream_byte_count);
RETURN(0);
OUT();
return 0;
}
/*
@ -102,7 +103,7 @@ int rhizome_bk2secret(rhizome_manifest *m,
bzero(xor_stream, sizeof xor_stream);
RETURN(rhizome_verify_bundle_privatekey(m,secret,bid));
OUT();
}
int rhizome_secret2bk(
@ -126,6 +127,7 @@ int rhizome_secret2bk(
bzero(xor_stream, sizeof xor_stream);
RETURN(0);
OUT();
}
@ -255,6 +257,7 @@ int rhizome_extract_privatekey(rhizome_manifest *m, rhizome_bk_t *bsk)
}
RETURN(result);
OUT();
}
/* Same as rhizome_extract_privatekey, except warnings become errors and are logged */
@ -339,6 +342,7 @@ int rhizome_find_bundle_author(rhizome_manifest *m)
if (config.debug.rhizome)
DEBUG("bundle author not found");
RETURN(1);
OUT();
}
/* Verify the validity of the manifest's secret key, ie, is the given manifest's 'cryptoSignSecret'
@ -380,6 +384,7 @@ int rhizome_verify_bundle_privatekey(rhizome_manifest *m,
m->haveSecret=EXISTING_BUNDLE_ID;
}
RETURN(0);
OUT();
}
int rhizome_sign_hash(rhizome_manifest *m,
@ -391,6 +396,7 @@ int rhizome_sign_hash(rhizome_manifest *m,
int ret=rhizome_sign_hash_with_key(m,m->cryptoSignSecret,m->cryptoSignPublic,out);
RETURN(ret);
OUT();
}
int rhizome_sign_hash_with_key(rhizome_manifest *m,const unsigned char *sk,
@ -413,6 +419,7 @@ int rhizome_sign_hash_with_key(rhizome_manifest *m,const unsigned char *sk,
out->signatureLength = 65 + crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES;
out->signature[0] = 0x17; // CryptoSign
RETURN(0);
OUT();
}
typedef struct manifest_signature_block_cache {
@ -474,6 +481,7 @@ int rhizome_manifest_lookup_signature_validity(unsigned char *hash,unsigned char
? -1 : 0;
}
RETURN(sig_cache[i].signature_valid);
OUT();
}
int rhizome_manifest_extract_signature(rhizome_manifest *m,int *ofs)
@ -544,6 +552,7 @@ int rhizome_manifest_extract_signature(rhizome_manifest *m,int *ofs)
(*ofs)+=len;
RETURN(0);
OUT();
}
// add value to nonce, with the same result regardless of CPU endian order

View File

@ -301,6 +301,7 @@ int rhizome_opendb()
// We can't delete a file that is being transferred in another process at this very moment...
RETURN(0);
OUT();
}
int rhizome_close_db()
@ -322,6 +323,7 @@ int rhizome_close_db()
}
rhizome_db=NULL;
RETURN(0);
OUT();
}
/* SQL query retry logic.
@ -710,6 +712,7 @@ rhizome_blob_handle *rhizome_database_open_blob_bybid(const char *id,
RETURN(NULL);
}
RETURN(rhizome_database_open_blob_byrowid(row_id,writeP));
OUT();
}
rhizome_blob_handle *rhizome_database_open_blob_byrowid(int row_id,int writeP)
@ -750,6 +753,7 @@ rhizome_blob_handle *rhizome_database_open_blob_byrowid(int row_id,int writeP)
// Couldn't open, so fail
free(blob);
RETURN(NULL);
OUT();
}
int rhizome_database_blob_close(rhizome_blob_handle *blob)
@ -774,6 +778,7 @@ int rhizome_database_blob_read(rhizome_blob_handle *blob,unsigned char *out,
if (r==count) RETURN(SQLITE_OK);
}
RETURN(-1);
OUT();
}
int rhizome_database_blob_write(rhizome_blob_handle *blob,unsigned char *buffer,
@ -792,6 +797,7 @@ int rhizome_database_blob_write(rhizome_blob_handle *blob,unsigned char *buffer,
if (r==count) RETURN(SQLITE_OK);
}
RETURN(-1);
OUT();
}
const char *rhizome_database_blob_errmsg(rhizome_blob_handle *blob)
@ -1271,6 +1277,7 @@ int rhizome_list_manifests(const char *service, const char *name,
cleanup:
sqlite3_finalize(statement);
RETURN(ret);
OUT();
}
int64_t rhizome_database_create_blob_for(const char *filehashhex_or_tempid,
@ -1348,6 +1355,7 @@ int64_t rhizome_database_create_blob_for(const char *filehashhex_or_tempid,
}
DEBUGF("Got rowid %lld for %s", rowid, filehashhex_or_tempid);
RETURN(rowid);
OUT();
}
void rhizome_bytes_to_hex_upper(unsigned const char *in, char *out, int byteCount)
@ -1657,4 +1665,5 @@ int rhizome_is_bar_interesting(unsigned char *bar){
if (lookup_time>50) WARNF("Looking up a BAR took %lldms",lookup_time);
RETURN(ret);
OUT();
}

View File

@ -611,6 +611,7 @@ static int schedule_fetch(struct rhizome_fetch_slot *slot)
slot->state=RHIZOME_FETCH_RXFILEMDP;
rhizome_fetch_switch_to_mdp(slot);
RETURN(0);
OUT();
}
/* Start fetching a bundle's payload ready for importing.
@ -1044,6 +1045,7 @@ int rhizome_suggest_queue_manifest_import(rhizome_manifest *m, const struct sock
}
RETURN(0);
OUT();
}
static int rhizome_fetch_close(struct rhizome_fetch_slot *slot)
@ -1170,6 +1172,7 @@ static int rhizome_fetch_mdp_requestblocks(struct rhizome_fetch_slot *slot)
rhizome_fetch_mdp_touch_timeout(slot);
RETURN(0);
OUT();
}
static int rhizome_fetch_mdp_requestmanifest(struct rhizome_fetch_slot *slot)
@ -1278,6 +1281,7 @@ static int rhizome_fetch_switch_to_mdp(struct rhizome_fetch_slot *slot)
}
RETURN(0);
OUT();
}
void rhizome_fetch_write(struct rhizome_fetch_slot *slot)
@ -1503,6 +1507,7 @@ int rhizome_write_content(struct rhizome_fetch_slot *slot, char *buffer, int byt
// slot is still open
RETURN(0);
OUT();
}
int rhizome_received_content(unsigned char *bidprefix,
@ -1541,6 +1546,7 @@ int rhizome_received_content(unsigned char *bidprefix,
}
RETURN(-1);
OUT();
}
void rhizome_fetch_poll(struct sched_ent *alarm)
@ -1717,4 +1723,5 @@ int unpack_http_response(char *response, struct http_response_parts *parts)
++p; // skip '\n' at end of blank line
parts->content_start = p;
RETURN(0);
OUT();
}

View File

@ -489,6 +489,7 @@ int http_header_complete(const char *buf, size_t len, size_t read_since_last_cal
}
}
RETURN(count == 2);
OUT();
}
int rhizome_direct_parse_http_request(rhizome_http_request *r);

View File

@ -111,6 +111,7 @@ int rhizome_manifest_to_bar(rhizome_manifest *m,unsigned char *bar)
else bar[RHIZOME_BAR_TTL_OFFSET]=0;
RETURN(0);
OUT();
}
int64_t rhizome_bar_version(unsigned char *bar)
@ -417,4 +418,5 @@ int overlay_rhizome_saw_advertisements(int i, struct overlay_frame *f, long long
sqlite_set_tracefunc(oldfunc);
RETURN(0);
OUT();
}

View File

@ -98,6 +98,7 @@ int rhizome_flush(struct rhizome_write *write){
DEBUGF("Written %lld of %lld", write->file_offset, write->file_length);
write->data_size=0;
RETURN(0);
OUT();
}
/* Expects file to be at least file_length in size */

View File

@ -139,6 +139,7 @@ int server(char *backing_file)
overlayServerMode();
RETURN(0);
OUT();
}
/* Called periodically by the server process in its main loop.

2
slip.c
View File

@ -316,7 +316,7 @@ int upper7_decode(struct slip_decode_state *state,unsigned char byte)
state->state=UPPER7_STATE_NOTINPACKET;
RETURN(0);
}
OUT();
}
/* state->src and state->src_size contain the freshly read bytes

2
vomp.c
View File

@ -301,6 +301,7 @@ static int store_jitter_sample(struct jitter_measurements *measurements, int sam
*delay=sample->delta - measurements->sorted_samples[0]->delta;
RETURN(0);
OUT();
}
static int get_jitter_size(struct jitter_measurements *measurements){
@ -313,6 +314,7 @@ static int get_jitter_size(struct jitter_measurements *measurements){
if (jitter < 60)
jitter=60;
RETURN(jitter);
OUT();
}
void set_codec_flag(int codec, unsigned char *flags){