Skip to content

Commit ab45c5e

Browse files
committed
cleanup getline buffer allocation and reuse
1 parent 795eff0 commit ab45c5e

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

avahi_browse.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,13 @@ int avahi_browse(struct service_tab *table,int tabsize,char const *service_name)
8787
exit(1);
8888

8989
int line_count;
90+
char *line = NULL;
91+
size_t linesize = 0;
92+
9093
for(line_count = 0; line_count < tabsize;){
9194
struct service_tab *tp = &table[line_count];
92-
char *line = NULL; // Fresh buffer for every line
93-
size_t linesize = 0;
9495

96+
// Allocates or reallocates as necessary
9597
if(getline(&line,&linesize,fp) <= 0){
9698
FREE(line);
9799
break;
@@ -115,11 +117,12 @@ int avahi_browse(struct service_tab *table,int tabsize,char const *service_name)
115117
deescape(tp->name);
116118
tp++;
117119
line_count++;
118-
} else {
119-
FREE(tp->buffer);
120-
// tp pointers are now invalid, but they get reset on next iteration or it passes out of scope
120+
line = NULL; // Force a new allocation by getline() on next iteration
121+
linesize = 0;
121122
}
123+
// Otherwise reuse line (with possible expansion)
122124
}
125+
FREE(line);
123126
pclose(fp); // What to do with return code?
124127
// Sort by instance entity name
125128
qsort(table,line_count,sizeof(table[0]),table_compare);

0 commit comments

Comments
 (0)