-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#348 Review usage of fgets in file reading #398
Conversation
merge to master
* make band limits configurable * allow showing out-of-band spots on bandmap
Would it make sense to have a function that reads a file and return the lines as an array of pointers? |
So you want to have new file dedicated only to read file and returning content of this file as array of |
IMHO checking the result of memory allocation according to different aspects can never be too much. :) |
The typical usage pattern is
The loop in Step 2 is surely generic. So one could even make a function for it
where This would be even better than reading-in the whole file. |
@zcsahok I think it worth to consider such functionality, but probably it's outside of this issue scope. We should/can discuss this in separate thread and create new issue for it. Such change would be bigger than just switch from fgets to getline. |
} | ||
|
||
fclose(cfp); | ||
|
||
if (s_inputbuffer > 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comparing a pointer and 0. It works, but using the typical check
if (s_inputbuffer != NULL)
would be clearer.
gchar *backupfile; | ||
gchar *cmd; | ||
char buffer[200]; | ||
char *buffer = NULL; | ||
size_t buffer_len = 200; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As buffer==NULL the assigned value is ignored by getline
, so do not set it.
See https://elixir.bootlin.com/glibc/latest/source/libio/iogetdelim.c#L62
char buffer[160]; | ||
int read; | ||
char *buffer = NULL; | ||
size_t buffer_len = 160; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not set buffer_len
.
struct stat statbuf; | ||
char inputbuffer[800]; | ||
char *inputbuffer = NULL; | ||
size_t read_len = 160; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not set read_len
.
@@ -332,8 +333,10 @@ void dxcc_add(char *dxcc_line) { | |||
/** load cty database from filename */ | |||
int load_ctydata(char *filename) { | |||
FILE *fd; | |||
char buf[181] = ""; | |||
char *buf = NULL; | |||
size_t buf_len = 181; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not set buf_len
.
struct ie_list *make_ie_list(char *file) { | ||
|
||
FILE *fp; | ||
char inputbuffer[91]; | ||
char *inputbuffer = NULL; | ||
size_t inputbuffer_len = 91; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not set inputbuffer_len
.
g_strchomp(buffer); /* drop trailing whitespace */ | ||
|
||
printf("%s\n", buffer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Private logging?
struct cabrillo_desc *cabdesc; | ||
char input_logfile[24]; | ||
char output_logfile[80], temp_logfile[80]; | ||
char logline[MAX_CABRILLO_LEN]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused MAX_CABRILLO_LEN from MAX_CABRILLO_LEN.
int qtcdirection = 0; | ||
int next_qtc_qso; | ||
int qsoflags_for_qtc[MAX_QSOS]; | ||
int nr_qtcsent = 0; | ||
|
||
int readqtccalls() { | ||
int s = 0; | ||
char inputbuffer[160]; | ||
char *inputbuffer = NULL; | ||
size_t inputbuffer_len = 160; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not set length.
country_list_raw = colon + 1; // skip optional contest name | ||
} else { | ||
country_list_raw = buffer; | ||
if (buffer != NULL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buffer is never NULL at this point. In case the previous calloc fails, then the following g_strlcpy (line 687) will already cause a segmentation violation.
@@ -747,7 +748,9 @@ int load_callmaster(void) { | |||
|
|||
FILE *cfp; | |||
char *callmaster_location; | |||
char s_inputbuffer[186] = ""; | |||
char *s_inputbuffer = NULL; | |||
size_t s_inputbuffer_len = 186; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not set length.
|
||
char buffer[160]; | ||
char *buffer = NULL; | ||
size_t buffer_len = 160; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do no set length.
My point was to avoid copy-pasting the same code to all affected source files. This can be done also later, no problem. This PR contains 43 changed files including 3 file mode changes. On the other hand there are only 12 files in So, I suggest to consolidate this PR as follows
In the meantime I am preparing a PR to allow unconditional application of astyle Then we check the feasibility of introducing a generic file reading function. Does this sound like a plan? |
Yes, thank you for all your comments. |
Removed all occurrences of fgets and replaced with getline.
Additionally code re-formated with astyle