From 25f74ad137493913303ee6f8be378e6f708a961d Mon Sep 17 00:00:00 2001 From: Marcin SP6MI Date: Wed, 2 Aug 2023 21:44:02 +0200 Subject: [PATCH] Update usage of getline, free memory and check if getline read something --- src/addmult.c | 42 +++++++---- src/bandmap.c | 97 ++++++++++++------------ src/cabrillo_utils.c | 49 +++++++------ src/checklogfile.c | 125 +++++++++++++++++-------------- src/dxcc.c | 28 ++++--- src/initial_exchange.c | 163 +++++++++++++++++++++-------------------- src/parse_logcfg.c | 50 +++++++------ src/readcabrillo.c | 9 ++- src/readcalls.c | 89 +++++++++++----------- src/readqtccalls.c | 74 +++++++++++-------- src/searchlog.c | 45 +++++++----- src/writecabrillo.c | 15 ++-- test/test_readcalls.c | 7 +- 13 files changed, 435 insertions(+), 358 deletions(-) diff --git a/src/addmult.c b/src/addmult.c index 6268622f7..3f6d39d0e 100644 --- a/src/addmult.c +++ b/src/addmult.c @@ -331,48 +331,58 @@ int init_and_load_multipliers(void) { FILE *cfp; char *s_inputbuffer = NULL; - size_t s_inputbuffer_len = 2000; + size_t s_inputbuffer_len = 0; char *mults_location; ssize_t read; if (mults_possible) { - /* free old array if exists */ - g_ptr_array_free(mults_possible, TRUE); + /* free old array if exists */ + g_ptr_array_free(mults_possible, TRUE); } mults_possible = g_ptr_array_new_with_free_func(free_possible_mult); if (strlen(multsfile) == 0) { - return 0; + return 0; } mults_location = find_available(multsfile); if ((cfp = fopen(mults_location, "r")) == NULL) { - mvprintw(9, 0, "Error opening multiplier file %s.\n", multsfile); - refreshp(); - sleep(5); + mvprintw(9, 0, "Error opening multiplier file %s.\n", multsfile); + refreshp(); + sleep(5); } g_free(mults_location); if (cfp == NULL) { - return 0; // couldn't open file + return 0; // couldn't open file } while ((read = getline(&s_inputbuffer, &s_inputbuffer_len, cfp)) != -1) { - /* strip leading and trailing whitespace */ - g_strstrip(s_inputbuffer); - - /* drop comments starting with '#' and empty lines */ - if (*s_inputbuffer == '#' || *s_inputbuffer == '\0') { - continue; + if (read != -1) { + if (s_inputbuffer_len > 0) { + /* strip leading and trailing whitespace */ + g_strstrip(s_inputbuffer); + + /* drop comments starting with '#' and empty lines */ + if (*s_inputbuffer == '#' || *s_inputbuffer == '\0') { + continue; + } + + add_mult_line(s_inputbuffer); + } + } + else { + printf("RuntimeError: %ld", read); + exit(1); } - - add_mult_line(s_inputbuffer); } fclose(cfp); + if (s_inputbuffer_len > 0) + free(s_inputbuffer); /* do not rely on the order in the mult file but sort it here */ g_ptr_array_sort(mults_possible, (GCompareFunc)cmp_size); diff --git a/src/bandmap.c b/src/bandmap.c index e906a7ae4..ee0d8f37b 100644 --- a/src/bandmap.c +++ b/src/bandmap.c @@ -130,7 +130,7 @@ void bmdata_read_file() { int timediff, last_bm_save_time, fc, read; char *line = NULL; char *token; - size_t line_len = 50; + size_t line_len = 0; static bool bmdata_parsed = false; if (bmdata_parsed) @@ -145,50 +145,57 @@ void bmdata_read_file() { if (timediff < 0) timediff = 0; - while ((read = getline(&line, &line_len, fp)) != -1) { - spot *entry = g_new0(spot, 1); - fc = 0; - token = strtok(line, ";"); - while (token != NULL) { - switch (fc) { - case 0: entry -> call = g_strdup(token); - break; - case 1: sscanf(token, "%d", &entry->freq); - break; - case 2: sscanf(token, "%hhd", &entry->mode); - break; - case 3: // re-evaluate band index - entry->bandindex = freq2bandindex(entry->freq); - break; - case 4: sscanf(token, "%c", &entry->node); - break; - case 5: sscanf(token, "%u", &entry->timeout); - break; - case 6: // dupe is transient, not read back - entry->dupe = false; - break; - case 7: sscanf(token, "%d", &entry->cqzone); - break; - case 8: sscanf(token, "%d", &entry->ctynr); - break; - case 9: entry->pfx = g_strdup(token); - break; - case 10: // mult is transient, not read back - entry->mult = false; - break; - } - fc++; - token = strtok(NULL, ";"); - } - if (entry->timeout > timediff) { - entry->timeout -= timediff; /* remaining time */ - allspots = g_list_insert_sorted(allspots, entry, (GCompareFunc)cmp_freq); - } else { - free_spot(entry); - } - } - } - fclose(fp); + while ((read = getline(&line, &line_len, fp)) != -1) { + if (line_len > 0) { + spot *entry = g_new0(spot, 1); + fc = 0; + token = strtok(line, ";"); + while (token != NULL) { + switch (fc) { + case 0: entry -> call = g_strdup(token); + break; + case 1: sscanf(token, "%d", &entry->freq); + break; + case 2: sscanf(token, "%hhd", &entry->mode); + break; + case 3: // re-evaluate band index + entry->bandindex = freq2bandindex(entry->freq); + break; + case 4: sscanf(token, "%c", &entry->node); + break; + case 5: sscanf(token, "%u", &entry->timeout); + break; + case 6: // dupe is transient, not read back + entry->dupe = false; + break; + case 7: sscanf(token, "%d", &entry->cqzone); + break; + case 8: sscanf(token, "%d", &entry->ctynr); + break; + case 9: entry->pfx = g_strdup(token); + break; + case 10: // mult is transient, not read back + entry->mult = false; + break; + } + fc++; + token = strtok(NULL, ";"); + } + + if (entry->timeout > timediff) { + entry->timeout -= timediff; /* remaining time */ + allspots = g_list_insert_sorted(allspots, entry, (GCompareFunc)cmp_freq); + } else { + free_spot(entry); + } + } + } + } + + if (line != NULL) + free(line); + + fclose(fp); } } diff --git a/src/cabrillo_utils.c b/src/cabrillo_utils.c index 20ebb916d..28bdadd5a 100644 --- a/src/cabrillo_utils.c +++ b/src/cabrillo_utils.c @@ -579,35 +579,40 @@ static int process_cabrillo_template_file(const char *file_name) { } char* logline = NULL; + size_t read_len = 0; int read; int result = PARSE_OK; - while ((read = getline(&logline, (size_t*)MAX_CABRILLO_LEN, fp)) != 1) { - g_strstrip(logline); - if (skip_template_line(logline)) { - continue; // skip it - } - char **fields = g_strsplit(logline, ":", 2); - g_strstrip(fields[0]); - if (g_strv_length(fields) == 2) { - g_strstrip(fields[1]); - } - - int rc = add_cabrillo_field(fields[0], fields[1]); - - if (rc != PARSE_OK) { - error_details = g_strdup_printf("unknown tag '%s'", fields[0]); - result = PARSE_ERROR; - } - - g_strfreev(fields); - - if (result != PARSE_OK) { - break; + while ((read = getline(&logline, &read_len, fp)) != 1) { + if (read_len > 0) { + g_strstrip(logline); + if (skip_template_line(logline)) { + continue; // skip it + } + char **fields = g_strsplit(logline, ":", 2); + g_strstrip(fields[0]); + if (g_strv_length(fields) == 2) { + g_strstrip(fields[1]); + } + + int rc = add_cabrillo_field(fields[0], fields[1]); + + if (rc != PARSE_OK) { + error_details = g_strdup_printf("unknown tag '%s'", fields[0]); + result = PARSE_ERROR; + } + + g_strfreev(fields); + + if (result != PARSE_OK) { + break; + } } } + if (logline != NULL) + free(logline); fclose(fp); return result; diff --git a/src/checklogfile.c b/src/checklogfile.c index 724496d6c..a64545b9b 100644 --- a/src/checklogfile.c +++ b/src/checklogfile.c @@ -89,18 +89,22 @@ int repair_log(char *filename) { } while((read = getline(&buffer, &buffer_len, infp)) != 1) { - /* strip trailing whitespace (and newline) */ - g_strchomp(buffer); + if (buffer_len > 0) { + /* strip trailing whitespace (and newline) */ + g_strchomp(buffer); - /* append spaces */ - fill = g_strnfill((LOGLINELEN - 1) - strlen(buffer), ' '); - strcat(buffer, fill); - g_free(fill); + /* append spaces */ + fill = g_strnfill((LOGLINELEN - 1) - strlen(buffer), ' '); + strcat(buffer, fill); + g_free(fill); - fputs(buffer, outfp); - fputs("\n", outfp); + fputs(buffer, outfp); + fputs("\n", outfp); + } } + if (buffer != NULL) + free(buffer); fclose(outfp); fclose(infp); g_free(backupfile); @@ -153,49 +157,53 @@ int checklogfile_new(char *filename) { tooshort = 0; while((read = getline(&buffer, &buffer_len, fp)) != -1) { - int band, linelen; - int bandok = 0; - - lineno++; - - /* if no logline -> complain and back */ - band = atoi(buffer); - - if ((band == 160) || - (band == 80) || - (band == 60) || - (band == 40) || - (band == 30) || - (band == 20) || - (band == 17) || - (band == 15) || - (band == 12) || - (band == 10)) - bandok = 1; - - if (!((buffer[0] == ';') || bandok)) { - /* msg no valid logline in line #, cannot handle it */ - shownr("No valid log line in line ", lineno); - return 1; - } + if (buffer_len > 0) { + int band, linelen; + int bandok = 0; + + lineno++; + + /* if no logline -> complain and back */ + band = atoi(buffer); + + if ((band == 160) || + (band == 80) || + (band == 60) || + (band == 40) || + (band == 30) || + (band == 20) || + (band == 17) || + (band == 15) || + (band == 12) || + (band == 10)) + bandok = 1; + + if (!((buffer[0] == ';') || bandok)) { + /* msg no valid logline in line #, cannot handle it */ + shownr("No valid log line in line ", lineno); + return 1; + } - linelen = strlen(buffer); + linelen = strlen(buffer); - /* if to long -> complain and back */ - if (linelen > LOGLINELEN) { - /* msg length of line # to long, - * cannot handle that log file format */ - shownr("Log line to long in line ", lineno); - showmsg("Can not handle that log format"); - return 1; - } + /* if to long -> complain and back */ + if (linelen > LOGLINELEN) { + /* msg length of line # to long, + * cannot handle that log file format */ + shownr("Log line to long in line ", lineno); + showmsg("Can not handle that log format"); + return 1; + } - /* if to short -> remember */ - if (linelen < LOGLINELEN) { - tooshort = 1; + /* if to short -> remember */ + if (linelen < LOGLINELEN) { + tooshort = 1; + } } } + if (buffer != NULL) + free(buffer); fclose(fp); if (tooshort) { @@ -253,21 +261,24 @@ void checklogfile(void) { } else { while ((read = getline(&inputbuffer, &read_len, infile)) != -1) { - - if (strlen(inputbuffer) != LOGLINELEN) { - /* append spaces */ - for (int i = strlen(inputbuffer); - i < LOGLINELEN; i++) { - - strcat(inputbuffer, " "); + if (read_len > 0) { + if (strlen(inputbuffer) != LOGLINELEN) { + /* append spaces */ + for (int i = strlen(inputbuffer); + i < LOGLINELEN; i++) { + + strcat(inputbuffer, " "); + } + + inputbuffer[LOGLINELEN - 1] = '\n'; + inputbuffer[LOGLINELEN] = '\0'; + } + fputs(inputbuffer, outfile); } - - inputbuffer[LOGLINELEN - 1] = '\n'; - inputbuffer[LOGLINELEN] = '\0'; - } - fputs(inputbuffer, outfile); } + if (inputbuffer != NULL) + free(inputbuffer); fclose(infile); fclose(outfile); } diff --git a/src/dxcc.c b/src/dxcc.c index 757b579a1..46f6bfb9a 100644 --- a/src/dxcc.c +++ b/src/dxcc.c @@ -347,20 +347,26 @@ int load_ctydata(char *filename) { dxcc_add("Not Specified : --: --: --: -00.00: 00.00: 0.0: :"); while ((read = getline(&buf, &buf_len, fd)) != -1) { - g_strchomp(buf); /* drop CR and/or NL and */ - if (*buf == '\0') /* ignore empty lines */ - continue; - - if (buf[0] != ' ') { // data line - dxcc_add(buf); - } else { // prefix line - loc = strtok(buf, " ,;"); - while (loc != NULL) { - prefix_add(loc); - loc = strtok(NULL, " ,;"); + if (buf_len > 0) { + g_strchomp(buf); /* drop CR and/or NL and */ + if (*buf == '\0') /* ignore empty lines */ + continue; + + if (buf[0] != ' ') { // data line + dxcc_add(buf); + } else { // prefix line + loc = strtok(buf, " ,;"); + while (loc != NULL) { + prefix_add(loc); + loc = strtok(NULL, " ,;"); + } } } } + + if (buf != NULL) + free(buf); + fclose(fd); return 0; } diff --git a/src/initial_exchange.c b/src/initial_exchange.c index 999e1498d..8276654d2 100644 --- a/src/initial_exchange.c +++ b/src/initial_exchange.c @@ -68,88 +68,91 @@ struct ie_list *make_ie_list(char *file) { showstring("Using initial exchange file", file); while ((read = getline(&inputbuffer, &inputbuffer_len, fp)) != -1) { - linectr++; - - g_strstrip(inputbuffer); // strip leading/trailing whitespace - - /* skip empty and comment lines */ - if (inputbuffer[0] == '#' || inputbuffer[0] == 0) { - continue; - } - - /* skip control directives like !!Order!!,... */ - if (inputbuffer[0] == '!') { - continue; - } - - if (strlen(inputbuffer) > 80) { - /* line to long */ - char msg[80]; - free_ie_list(ie_listhead); - fclose(fp); - sprintf(msg, "Line %d: too long", linectr); - showmsg(msg); - return NULL; - } - - loc = strchr(inputbuffer, ','); - - if (loc == NULL) { - /* no comma found */ - char msg[80]; - free_ie_list(ie_listhead); - fclose(fp); - sprintf(msg, "Line %d: no comma found", linectr); - showmsg(msg); - return NULL; - } - - // comma found, parse the line - new = malloc(sizeof(struct ie_list)); - - if (new == NULL) { - free_ie_list(ie_listhead); - fclose(fp); - showmsg("Out of memory"); - return NULL; - } - - *loc = '\0'; /* split the string into call and exchange */ - - token = strtok(inputbuffer, " \t"); /* callsign is first - token delimited by - whitespace */ - if (token == NULL || strtok(NULL, " \t")) { - /* 0 or >1 token before comma */ - char msg[80]; - free(new); - free_ie_list(ie_listhead); - fclose(fp); - sprintf(msg, "Line %d: 0 or more than one token before comma", - linectr); - showmsg(msg); - return NULL; - } - - strncpy(new->call, token, MAX_CALL_LENGTH); - new->call[MAX_CALL_LENGTH] = '\0'; /* proper termination */ - - // prepare exchange field - char *xchg = loc + 1; - loc = strchr(xchg, ','); - if (loc != NULL) { - *loc = '\0'; /* terminate it at the 2nd comma */ - } - g_strstrip(xchg); // strip leading/trailing whitespace - strncpy(new->exchange, xchg, MAX_IE_LENGTH); - new->exchange[MAX_IE_LENGTH] = '\0'; /* proper termination */ - - /* prepend new entry to existing list */ - new->next = ie_listhead; - ie_listhead = new; - + if (inputbuffer_len > 0) { + linectr++; + + g_strstrip(inputbuffer); // strip leading/trailing whitespace + + /* skip empty and comment lines */ + if (inputbuffer[0] == '#' || inputbuffer[0] == 0) { + continue; + } + + /* skip control directives like !!Order!!,... */ + if (inputbuffer[0] == '!') { + continue; + } + + if (strlen(inputbuffer) > 80) { + /* line to long */ + char msg[80]; + free_ie_list(ie_listhead); + fclose(fp); + sprintf(msg, "Line %d: too long", linectr); + showmsg(msg); + return NULL; + } + + loc = strchr(inputbuffer, ','); + + if (loc == NULL) { + /* no comma found */ + char msg[80]; + free_ie_list(ie_listhead); + fclose(fp); + sprintf(msg, "Line %d: no comma found", linectr); + showmsg(msg); + return NULL; + } + + // comma found, parse the line + new = malloc(sizeof(struct ie_list)); + + if (new == NULL) { + free_ie_list(ie_listhead); + fclose(fp); + showmsg("Out of memory"); + return NULL; + } + + *loc = '\0'; /* split the string into call and exchange */ + + token = strtok(inputbuffer, " \t"); /* callsign is first + token delimited by + whitespace */ + if (token == NULL || strtok(NULL, " \t")) { + /* 0 or >1 token before comma */ + char msg[80]; + free(new); + free_ie_list(ie_listhead); + fclose(fp); + sprintf(msg, "Line %d: 0 or more than one token before comma", + linectr); + showmsg(msg); + return NULL; + } + + strncpy(new->call, token, MAX_CALL_LENGTH); + new->call[MAX_CALL_LENGTH] = '\0'; /* proper termination */ + + // prepare exchange field + char *xchg = loc + 1; + loc = strchr(xchg, ','); + if (loc != NULL) { + *loc = '\0'; /* terminate it at the 2nd comma */ + } + g_strstrip(xchg); // strip leading/trailing whitespace + strncpy(new->exchange, xchg, MAX_IE_LENGTH); + new->exchange[MAX_IE_LENGTH] = '\0'; /* proper termination */ + + /* prepend new entry to existing list */ + new->next = ie_listhead; + ie_listhead = new; + } } + if (inputbuffer != NULL) + free(inputbuffer); fclose(fp); return ie_listhead; diff --git a/src/parse_logcfg.c b/src/parse_logcfg.c index 9450dfbdc..413477efc 100644 --- a/src/parse_logcfg.c +++ b/src/parse_logcfg.c @@ -114,17 +114,21 @@ int parse_configfile(FILE *fp) { ssize_t read; while ((read = getline(&buffer, &buffer_len, fp)) != -1) { - g_strchug(buffer); // remove leading space - if (isCommentLine(buffer)) { // skip comments and empty lines - continue; - } + if (buffer_len > 0) { + g_strchug(buffer); // remove leading space + if (isCommentLine(buffer)) { // skip comments and empty lines + continue; + } - status = parse_logcfg(buffer); - if (status != PARSE_OK) { - break; - } - } + status = parse_logcfg(buffer); + if (status != PARSE_OK) { + break; + } + } + } + if (buffer != NULL) + free(buffer); return status; } @@ -682,13 +686,15 @@ static int cfg_countrylist(const cfg_arg_t arg) { char *prefix = g_strdup_printf("%s:", whichcontest); while ((read = getline(&buffer, &buffer_len, fp)) != -1) { - g_strstrip(buffer); /* no leading/trailing whitespace*/ + if (buffer_len > 0) { + g_strstrip(buffer); /* no leading/trailing whitespace*/ - /* accept only a line starting with the contest name - * (CONTEST=) followed by ':' */ - if (strncasecmp(buffer, prefix, strlen(prefix)) == 0) { - country_list_raw = buffer + strlen(prefix); // skip prefix - break; + /* accept only a line starting with the contest name + * (CONTEST=) followed by ':' */ + if (strncasecmp(buffer, prefix, strlen(prefix)) == 0) { + country_list_raw = buffer + strlen(prefix); // skip prefix + break; + } } } @@ -773,12 +779,14 @@ static int cfg_continentlist(const cfg_arg_t arg) { if ((fp = fopen(buffer, "r")) != NULL) { char *prefix = g_strdup_printf("%s:", whichcontest); while((read = getline(&buffer, &buffer_len, fp)) != -1) { - g_strstrip(buffer); /* no leading/trailing whitespace*/ - /* accept only a line starting with the contest name - * (CONTEST=) followed by ':' */ - if (strncasecmp(buffer, prefix, strlen(prefix)) == 0) { - cont_multiplier_list = buffer + strlen(prefix); // skip prefix - break; + if (buffer_len > 0) { + g_strstrip(buffer); /* no leading/trailing whitespace*/ + /* accept only a line starting with the contest name + * (CONTEST=) followed by ':' */ + if (strncasecmp(buffer, prefix, strlen(prefix)) == 0) { + cont_multiplier_list = buffer + strlen(prefix); // skip prefix + break; + } } } diff --git a/src/readcabrillo.c b/src/readcabrillo.c index 65714a084..d1a49e85a 100644 --- a/src/readcabrillo.c +++ b/src/readcabrillo.c @@ -396,6 +396,7 @@ int readcabrillo(int mode) { char output_logfile[80], temp_logfile[80]; char* logline = NULL; char *tempstrp; + size_t read_len = 0; char t_qsonrstr[5]; int t_qsonum; @@ -475,14 +476,16 @@ int readcabrillo(int mode) { init_qso_array(); - while((read = getline(&logline, (size_t*)MAX_CABRILLO_LEN, fp1)) != 1) { - cab_qso_to_tlf(logline, cabdesc); + while((read = getline(&logline, &read_len, fp1)) != 1) { + if (read_len > 0) + cab_qso_to_tlf(logline, cabdesc); } strcpy(qsonrstr, t_qsonrstr); qsonum = t_qsonum; bandinx = t_bandinx; - + if (logline != NULL) + free(logline); fclose(fp1); free_cabfmt(cabdesc); strcpy(logfile, temp_logfile); diff --git a/src/readcalls.c b/src/readcalls.c index c97ccb4a2..720183e8c 100644 --- a/src/readcalls.c +++ b/src/readcalls.c @@ -121,10 +121,10 @@ static void show_progress(int linenr) { int readcalls(const char *logfile, bool interactive) { char* inputbuffer = NULL; - size_t inputbuffer_len = LOGLINELEN + 1; + size_t inputbuffer_len = 0; struct qso_t *qso; int linenr = 0; - int read; + ssize_t read; FILE *fp; @@ -143,50 +143,53 @@ int readcalls(const char *logfile, bool interactive) { bool log_changed = false; while ((read = getline(&inputbuffer, &inputbuffer_len, fp)) != -1) { - // drop trailing newline - inputbuffer[LOGLINELEN - 1] = '\0'; + if (inputbuffer_len > 0) { + // drop trailing newline + inputbuffer[inputbuffer_len - 1] = '\0'; + linenr++; + + if (interactive) { + show_progress(linenr); + } + + qso = parse_qso(inputbuffer); + + if (qso->is_comment) { + g_ptr_array_add(qso_array, qso); + continue; /* skip further processing for note entry */ + } + + /* get the country number, not known at this point */ + countrynr = getctydata(qso->call); + checkexchange(qso, false); + if (qso->normalized_comment != NULL && strlen(qso->normalized_comment) > 0) { + strcpy(qso->comment, qso->normalized_comment); + } + + dupe = is_dupe(qso->call, qso->bandindex, qso->mode); + addcall(qso); + score_qso(qso); + char *logline = makelogline(qso); + +// Ignore new line character at end of line in qso->logline + if (strcmp(logline, strtok(qso->logline, "\n")) != 0) { + g_free(qso->logline); + qso->logline = g_strdup(logline); + log_changed = true; + } + + g_free(logline); + + if(inputbuffer != NULL) + free(inputbuffer); + + // drop transient fields + FREE_DYNAMIC_STRING(qso->callupdate); + FREE_DYNAMIC_STRING(qso->normalized_comment); + FREE_DYNAMIC_STRING(qso->section); - linenr++; - - if (interactive) { - show_progress(linenr); - } - - qso = parse_qso(inputbuffer); - - if (qso->is_comment) { g_ptr_array_add(qso_array, qso); - continue; /* skip further processing for note entry */ - } - - /* get the country number, not known at this point */ - countrynr = getctydata(qso->call); - checkexchange(qso, false); - if (qso->normalized_comment != NULL && strlen(qso->normalized_comment) > 0) { - strcpy(qso->comment, qso->normalized_comment); } - dupe = is_dupe(qso->call, qso->bandindex, qso->mode); - - addcall(qso); - score_qso(qso); - - char *logline = makelogline(qso); - - if (strcmp(logline, qso->logline) != 0) { - // different: update log line and mark change - g_free(qso->logline); - qso->logline = g_strdup(logline); - log_changed = true; - } - - g_free(logline); - - // drop transient fields - FREE_DYNAMIC_STRING(qso->callupdate); - FREE_DYNAMIC_STRING(qso->normalized_comment); - FREE_DYNAMIC_STRING(qso->section); - - g_ptr_array_add(qso_array, qso); } fclose(fp); diff --git a/src/readqtccalls.c b/src/readqtccalls.c index db11767e6..61d347fee 100644 --- a/src/readqtccalls.c +++ b/src/readqtccalls.c @@ -63,31 +63,36 @@ int readqtccalls() { } while ((read = getline(&inputbuffer, &inputbuffer_len, fp)) != -1) { - s++; - - /* find maximum sent QTC block serial */ - g_strlcpy(temps, inputbuffer + 50, 5); // get serial of QTC block - tempi = atoi(temps); - if (tempi > nr_qtcsent) { - nr_qtcsent = tempi; + if (inputbuffer_len > 0) { + s++; + + /* find maximum sent QTC block serial */ + g_strlcpy(temps, inputbuffer + 50, 5); // get serial of QTC block + tempi = atoi(temps); + if (tempi > nr_qtcsent) { + nr_qtcsent = tempi; + } + + /* mark corresponding qso line as used for QTC */ + g_strlcpy(temps, inputbuffer + 12, 5); // qso nr in qso list + tempi = atoi(temps) - 1; + qsoflags_for_qtc[tempi] = 1; + + /* remember callsign, build number of sent QTCs */ + parse_qtcline(inputbuffer, callsign, SEND); + qtc_inc(callsign, SEND); + + total++; /* add one point per QTC */ + + /* find first unused QSO number for QTCs */ + if (tempi > last_qtc) { + last_qtc = tempi; + } } + } - /* mark corresponding qso line as used for QTC */ - g_strlcpy(temps, inputbuffer + 12, 5); // qso nr in qso list - tempi = atoi(temps) - 1; - qsoflags_for_qtc[tempi] = 1; - - /* remember callsign, build number of sent QTCs */ - parse_qtcline(inputbuffer, callsign, SEND); - qtc_inc(callsign, SEND); - - total++; /* add one point per QTC */ - - /* find first unused QSO number for QTCs */ - if (tempi > last_qtc) { - last_qtc = tempi; - } - } + if (inputbuffer != NULL) + free(inputbuffer); next_qtc_qso = last_qtc; @@ -113,13 +118,17 @@ int readqtccalls() { } while ((read = getline(&inputbuffer, &inputbuffer_len, fp)) != -1) { - /* remember callsign, build number of received QTCs */ - parse_qtcline(inputbuffer, callsign, RECV); - qtc_inc(callsign, RECV); + if (inputbuffer_len > 0) { + /* remember callsign, build number of received QTCs */ + parse_qtcline(inputbuffer, callsign, RECV); + qtc_inc(callsign, RECV); - total++; /* add one point per QTC */ + total++; /* add one point per QTC */ + } } + if (inputbuffer != NULL) + free(inputbuffer); fclose(fp); } @@ -134,9 +143,12 @@ int readqtccalls() { while ((read = getline(&inputbuffer, &inputbuffer_len, fp)) != -1) { /* remember callsign, mark it as QTC capable, based on eg. last years */ - qtc_inc(g_strstrip(inputbuffer), QTC_CAP); + if (inputbuffer_len > 0) + qtc_inc(g_strstrip(inputbuffer), QTC_CAP); } + if (inputbuffer != NULL) + free(inputbuffer); fclose(fp); } @@ -147,8 +159,12 @@ int readqtccalls() { } else { while ((read = getline(&inputbuffer, &inputbuffer_len, fp)) != -1) { /* remember callsign, set marked QTC states */ - parse_qtc_flagline(inputbuffer); + if (inputbuffer_len > 0) + parse_qtc_flagline(inputbuffer); } + + if (inputbuffer != NULL) + free(inputbuffer); fclose(fp); } diff --git a/src/searchlog.c b/src/searchlog.c index 979e9502d..09e622d56 100644 --- a/src/searchlog.c +++ b/src/searchlog.c @@ -768,39 +768,44 @@ int load_callmaster(void) { GHashTable *callset = g_hash_table_new(g_str_hash, g_str_equal); while((read = getline(&s_inputbuffer, &s_inputbuffer_len, cfp)) != -1) { - g_strstrip(s_inputbuffer); + if (s_inputbuffer_len > 0) { + g_strstrip(s_inputbuffer); - /* skip comment lines and calls shorter than 3 chars */ - if (s_inputbuffer[0] == '#' || strlen(s_inputbuffer) < 3) { - continue; - } + /* skip comment lines and calls shorter than 3 chars */ + if (s_inputbuffer[0] == '#' || strlen(s_inputbuffer) < 3) { + continue; + } - /* store version */ - if (strlen(s_inputbuffer) == 11 && strncmp(s_inputbuffer, "VER", 3) == 0) { - strcpy(callmaster_version, s_inputbuffer); // save it - } + /* store version */ + if (strlen(s_inputbuffer) == 11 && strncmp(s_inputbuffer, "VER", 3) == 0) { + strcpy(callmaster_version, s_inputbuffer); // save it + } - char *call = g_ascii_strup(s_inputbuffer, 11); + char *call = g_ascii_strup(s_inputbuffer, 11); - if (CONTEST_IS(ARRL_SS)) { - /* keep only NA stations */ - if (strchr("AKWVCN", call[0]) == NULL) { + if (CONTEST_IS(ARRL_SS)) { + /* keep only NA stations */ + if (strchr("AKWVCN", call[0]) == NULL) { + g_free(call); + continue; + } + } + + if (g_hash_table_contains(callset, call)) { // already have this call g_free(call); continue; } - } - if (g_hash_table_contains(callset, call)) { // already have this call - g_free(call); - continue; + g_hash_table_add(callset, call); + g_ptr_array_add(callmaster, call); } - - g_hash_table_add(callset, call); - g_ptr_array_add(callmaster, call); } g_hash_table_destroy(callset); + if (s_inputbuffer != NULL) + free(s_inputbuffer); + fclose(cfp); return callmaster->len; } diff --git a/src/writecabrillo.c b/src/writecabrillo.c index 480c09542..a882e6914 100644 --- a/src/writecabrillo.c +++ b/src/writecabrillo.c @@ -100,13 +100,14 @@ struct linedata_t *get_next_record(FILE *fp) { int read; while ((read = getline(&buffer, &buffer_len, fp)) != -1) { - if (!log_is_comment(buffer)) { - ptr = parse_logline(buffer); - return ptr; - } - } - - free(buffer); + if (buffer_len > 0) + if (!log_is_comment(buffer)) { + ptr = parse_logline(buffer); + return ptr; + } + } + if (buffer != NULL) + free(buffer); return NULL; } diff --git a/test/test_readcalls.c b/test/test_readcalls.c index 9c182eafe..b51f9e95b 100644 --- a/test/test_readcalls.c +++ b/test/test_readcalls.c @@ -195,8 +195,7 @@ void test_qso_array_init(void **state) { void test_readcalls_simple_log(void **state) { int lines; - gchar *qso_line = g_strndup(QSO1, LOGLINELEN - 1); - + gchar *qso_line = g_strndup(QSO1, 87); write_log(LOGFILE); lines = readcalls(LOGFILE, true); assert_non_null(qso_array); @@ -211,7 +210,7 @@ void test_readcalls_simple_log(void **state) { } void test_readcalls_note(void **state) { - gchar *note = g_strndup(NOTE, LOGLINELEN - 1); + gchar *note = g_strndup(NOTE, LOGLINELEN); write_log(LOGFILE); append_log_line(LOGFILE, NOTE); @@ -233,7 +232,7 @@ void test_add_to_worked(void **state) { assert_int_equal(nr_worked, 1); assert_string_equal(worked[0].call, "PY9BBB"); assert_string_equal(worked[0].exchange, "15"); - time_t ts = parse_time(QSO1 + 7, DATE_TIME_FORMAT); + time_t ts = parse_time(QSO1 + 7, DATE_TIME_FORMAT); // string starts from date: 12-Jan-18 16:34 assert_int_equal(worked[0].qsotime[SSBMODE][BANDINDEX_80], ts); assert_int_equal(get_nr_of_points(), 3); assert_int_equal(get_nr_of_mults(), 2);