Skip to content

Commit

Permalink
TLV length extensions; FREE macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ka9q committed Apr 4, 2023
1 parent 0ace090 commit a6da2e5
Show file tree
Hide file tree
Showing 33 changed files with 277 additions and 147 deletions.
12 changes: 11 additions & 1 deletion airspyd.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,17 @@ void decode_airspy_commands(struct sdrstate *sdr,uint8_t *buffer,int length){
if(type == EOL)
break; // End of list

unsigned int const optlen = *cp++;
unsigned int optlen = *cp++;
if(optlen & 0x80){
// length is >= 128 bytes; fetch actual length from next N bytes, where N is low 7 bits of optlen
int length_of_length = optlen & 0x7f;
optlen = 0;
while(length_of_length > 0){
optlen <<= 8;
optlen |= *cp++;
length_of_length--;
}
}
if(cp - buffer + optlen >= length)
break; // Invalid length

Expand Down
12 changes: 11 additions & 1 deletion airspyhfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,17 @@ void decode_airspyhf_commands(struct sdrstate *sdr,uint8_t *buffer,int length){
if(type == EOL)
break; // End of list

unsigned int const optlen = *cp++;
unsigned int optlen = *cp++;
if(optlen & 0x80){
// length is >= 128 bytes; fetch actual length from next N bytes, where N is low 7 bits of optlen
int length_of_length = optlen & 0x7f;
optlen = 0;
while(length_of_length > 0){
optlen <<= 8;
optlen |= *cp++;
length_of_length--;
}
}
if(cp - buffer + optlen >= length)
break; // Invalid length

Expand Down
2 changes: 1 addition & 1 deletion aprsfeed.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,6 @@ void *netreader(void *arg){
if(Logfile)
fwrite(line,linelen,1,Logfile);
}
free(line); line = NULL;
FREE(line);
return NULL;
}
7 changes: 4 additions & 3 deletions attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <assert.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include "misc.h"
#include "attr.h"

// Look for external attribute "name" on an open file and perform scanf on its value
Expand All @@ -27,7 +28,7 @@ int attrscanf(int fd,char const *name,char const *format, ...){
fgetxattr(fd,fullname,value,attrlen);
value[attrlen] = '\0';
}
free(fullname); fullname = NULL;
FREE(fullname);
}
#else // mainly OSX, probably BSD
if((attrlen = fgetxattr(fd,name,NULL,0,0,0)) >= 0){
Expand Down Expand Up @@ -60,12 +61,12 @@ int attrprintf(int fd,char const *attr,char const *format, ...){
char *prefix = NULL;
if(asprintf(&prefix,"user.%s",attr) >= 0){
r = fsetxattr(fd,prefix,args,argslen,0);
free(prefix); prefix = NULL;
FREE(prefix);
}
#else
r = fsetxattr(fd,attr,args,argslen,0,0);
#endif
free(args); args = NULL;
FREE(args);
}
va_end(ap);
return r;
Expand Down
10 changes: 5 additions & 5 deletions avahi.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ static void *avahi_register(void *p){
break;
}
// Get here only on early failure; give up completely
free(userdata->service_name);
free(userdata->service_type);
free(userdata->description);
free(userdata->dns_name);
free(userdata);
FREE(userdata->service_name);
FREE(userdata->service_type);
FREE(userdata->description);
FREE(userdata->dns_name);
FREE(userdata);
pthread_exit(NULL);
}
static void client_callback(AvahiClient *c, AvahiClientState state, void *p) {
Expand Down
15 changes: 12 additions & 3 deletions control.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,7 @@ int main(int argc,char *argv[]){
demods[i].tune.freq);
}
fprintf(stdout,"Total SSRCs: %u\n",ssrc_count);
free(demods);
demods = NULL;
FREE(demods);
exit(0);
}

Expand Down Expand Up @@ -982,7 +981,17 @@ static int for_us(struct demod *demod,uint8_t const *buffer,int length,uint32_t
if(type == EOL)
break; // end of list, no length

unsigned int const optlen = *cp++;
unsigned int optlen = *cp++;
if(optlen & 0x80){
// length is >= 128 bytes; fetch actual length from next N bytes, where N is low 7 bits of optlen
int length_of_length = optlen & 0x7f;
optlen = 0;
while(length_of_length > 0){
optlen <<= 8;
optlen |= *cp++;
length_of_length--;
}
}
if(cp - buffer + optlen >= length)
break; // invalid length; we can't continue to scan

Expand Down
12 changes: 11 additions & 1 deletion decode_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,17 @@ int decode_fe_status(struct frontend *frontend,uint8_t const *buffer,int length)
if(type == EOL)
break; // end of list

unsigned int const optlen = *cp++;
unsigned int optlen = *cp++;
if(optlen & 0x80){
// length is >= 128 bytes; fetch actual length from next N bytes, where N is low 7 bits of optlen
int length_of_length = optlen & 0x7f;
optlen = 0;
while(length_of_length > 0){
optlen <<= 8;
optlen |= *cp++;
length_of_length--;
}
}
if(cp - buffer + optlen >= length)
break; // invalid length; we can't continue to scan

Expand Down
16 changes: 13 additions & 3 deletions dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ void dump_metadata(uint8_t const * const buffer,int length){
if(type == EOL)
break; // End of list

unsigned int const optlen = *cp++;
unsigned int optlen = *cp++;
if(optlen & 0x80){
// length is >= 128 bytes; fetch actual length from next N bytes, where N is low 7 bits of optlen
int length_of_length = optlen & 0x7f;
optlen = 0;
while(length_of_length > 0){
optlen <<= 8;
optlen |= *cp++;
length_of_length--;
}
}
if(cp - buffer + optlen >= length)
break; // Invalid length
printf(" (%d) ",type);
Expand Down Expand Up @@ -360,14 +370,14 @@ void dump_metadata(uint8_t const * const buffer,int length){
printf("noncoherent bin bandwidth %.1f Hz",decode_float(cp,optlen));
break;
case BIN_COUNT:
printf("bin count %d",(int)decode_int(cp,optlen));
printf("bins %d",(int)decode_int(cp,optlen));
break;
case INTEGRATE_TC:
printf("integrate tc %.1f s",decode_float(cp,optlen));
break;
case BIN_DATA:
{
printf("bin data:");
printf("fft bins:");
int count = optlen/sizeof(float);
for(int i=0; i < count; i++){
printf(" %.0f",decode_float(cp,sizeof(float)));
Expand Down
11 changes: 4 additions & 7 deletions filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,10 @@ void *run_fft(void *p){
pthread_mutex_unlock(job->completion_mutex);

// Do this after signaling in case free() takes time
if(job->input){
free(job->input);
job->input = NULL; // free input but NOT output
}
FREE(job->input); // free input but NOT output

bool const terminate = job->terminate; // Don't use job pointer after free
free(job); job = NULL;
FREE(job);
if(terminate)
break; // Terminate after this job
}
Expand Down Expand Up @@ -606,7 +603,7 @@ int delete_filter_input(struct filter_in ** p){
fftwf_free(master->input_buffer.c);
for(int i=0; i < ND; i++)
fftwf_free(master->fdomain[i]);
free(master);
FREE(master);
return 0;
}
int delete_filter_output(struct filter_out **p){
Expand All @@ -623,7 +620,7 @@ int delete_filter_output(struct filter_out **p){
fftwf_free(slave->output_buffer.c);
fftwf_free(slave->response);
fftwf_free(slave->fdomain);
free(slave);
FREE(slave);
return 0;
}

Expand Down
4 changes: 1 addition & 3 deletions fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,7 @@ void *demod_fm(void *arg){
break; // no valid output stream; terminate!
} // while(!demod->terminate)
quit:;
if(demod->filter.energies)
free(demod->filter.energies);
demod->filter.energies = NULL;
FREE(demod->filter.energies);
delete_filter_output(&demod->filter.out);
return NULL;
}
14 changes: 12 additions & 2 deletions funcubed.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ int main(int argc,char *argv[]){
}
if(calfp)
fclose(calfp);
free(calfilename);
FREE(calfilename);
}
}
// Config file overrides state save file
Expand Down Expand Up @@ -631,7 +631,17 @@ void decode_fcd_commands(struct sdrstate *sdr, uint8_t const *buffer,int length)
if(type == EOL)
break; // End of list

unsigned int const optlen = *cp++;
unsigned int optlen = *cp++;
if(optlen & 0x80){
// length is >= 128 bytes; fetch actual length from next N bytes, where N is low 7 bits of optlen
int length_of_length = optlen & 0x7f;
optlen = 0;
while(length_of_length > 0){
optlen <<= 8;
optlen |= *cp++;
length_of_length--;
}
}
if(cp - buffer + optlen >= length)
break; // Invalid length

Expand Down
4 changes: 1 addition & 3 deletions linear.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ void *demod_linear(void *arg){
}
}
quit:;
if(demod->filter.energies)
free(demod->filter.energies);
demod->filter.energies = NULL;
FREE(demod->filter.energies);
delete_filter_output(&demod->filter.out);
return NULL;
}
7 changes: 2 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,7 @@ static int loadconfig(char const * const file){
demod = ndemod;
ndemod = NULL;
}
free(freq_list);
freq_list = NULL;
FREE(freq_list);
}
free_demod(&demod); // last one wasn't needed
fprintf(stdout,"%d demodulators started\n",nfreq);
Expand Down Expand Up @@ -546,9 +545,7 @@ void *rtcp_send(void *arg){
strlcpy(sdes[0].message,string,sizeof(sdes[0].message));
sdes[0].mlen = strlen(sdes[0].message);
}
if(string){
free(string); string = NULL;
}
FREE(string);

sdes[1].type = NAME;
strlcpy(sdes[1].message,"KA9Q Radio Program",sizeof(sdes[1].message));
Expand Down
14 changes: 4 additions & 10 deletions misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <limits.h>
#include <complex.h>
#include <math.h> // Get M_PI
#include <stdlib.h> // for ldiv()
#include <stdlib.h> // for ldiv(), free()

#ifndef M_PIf
#define M_PIf ((float)(M_PI))
Expand Down Expand Up @@ -70,10 +70,6 @@ int pthread_barrier_wait(pthread_barrier_t *barrier);

#endif // ifdef __APPLE__





// Stolen from the Linux kernel -- enforce type matching of arguments
#define min(x,y) ({ \
typeof(x) _x = (x); \
Expand Down Expand Up @@ -129,8 +125,6 @@ float i1(float const z); // 1st kind
float xi(float thetasq);
float fm_snr(float r);



static int16_t inline scaleclip(float const x){
if(x >= 1.0)
return SHRT_MAX;
Expand Down Expand Up @@ -233,22 +227,22 @@ static inline time_t gps_time_sec(void){
return utc_time_sec() - (UNIX_EPOCH - GPS_UTC_OFFSET);
}



// Return time of day as nanosec from UTC epoch
static inline int64_t utc_time_ns(void){
struct timespec now;
clock_gettime(CLOCK_REALTIME,&now);
return ts2ns(&now);
}


// Return time of day as nanosec from GPS epoch
// Note: assumes fixed leap second offset
// Could be better derived direct from a GPS receiver without applying the leap second offset
static inline int64_t gps_time_ns(void){
return utc_time_ns() - BILLION * (UNIX_EPOCH - GPS_UTC_OFFSET);
}

// How the free() library routine should have been all along: null the pointer after freeing!
#define FREE(p) (free(p), p = NULL)


#endif // _MISC_H
20 changes: 6 additions & 14 deletions monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,7 @@ static void decode_task_cleanup(void *arg){
struct packet *pkt_next;
for(struct packet *pkt = sp->queue; pkt; pkt = pkt_next){
pkt_next = pkt->next;
free(pkt);
pkt = NULL;
FREE(pkt);
}
}

Expand Down Expand Up @@ -939,14 +938,8 @@ static void *decode_task(void *arg){
sp->tot_active += (float)sp->frame_size / sp->samprate;
sp->active += (float)sp->frame_size / sp->samprate;
endloop:;
if(bounce != NULL){
free(bounce);
bounce = NULL;
}
if(pkt != NULL){
free(pkt);
pkt = NULL;
}
FREE(bounce);
FREE(pkt);
} // !sp->terminate
pthread_cleanup_pop(1);
return NULL;
Expand Down Expand Up @@ -993,8 +986,7 @@ static void *display(void *arg){
while(getline(&line,&size,fp) != -1){
addstr(line);
}
free(line);
line = NULL;
FREE(line);
fclose(fp);
fp = NULL;
}
Expand Down Expand Up @@ -1410,7 +1402,7 @@ static struct session *create_session(void){
static int close_session(struct session **p){
if(p == NULL)
return -1;
struct session * const sp = *p;
struct session * sp = *p;
if(sp == NULL)
return -1;
assert(Nsessions > 0);
Expand All @@ -1420,7 +1412,7 @@ static int close_session(struct session **p){
if(Sessions[i] == sp){
Nsessions--;
memmove(&Sessions[i],&Sessions[i+1],(Nsessions-i) * sizeof(Sessions[0]));
free(sp);
FREE(sp);
*p = NULL;
return 0;
}
Expand Down
Loading

0 comments on commit a6da2e5

Please sign in to comment.