summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTauwasser <Tauwasser@tauwasser.eu>2018-06-03 22:14:23 +0200
committerTauwasser <Tauwasser@tauwasser.eu>2018-06-03 22:14:23 +0200
commitb50c850650727689a60689db2fa63f2cd92ddd09 (patch)
tree1960883b42b8a0297dbbc40317359c2ac3eb49f1
parent5d65c77bd0b356c2d5659e6029f712131bb697b6 (diff)
tools/make_shim: fix use of getline
Replace with fgets, which should be safe even if it reads an internal NUL char, as all other functions will just cut the string off in that case. Signed-off-by: Tauwasser <Tauwasser@tauwasser.eu>
-rw-r--r--tools/make_shim.c22
1 files changed, 10 insertions, 12 deletions
diff --git a/tools/make_shim.c b/tools/make_shim.c
index 8fdd0e7..e4019cf 100644
--- a/tools/make_shim.c
+++ b/tools/make_shim.c
@@ -58,14 +58,13 @@ size_t strrcspn(const char *s1, const char *s2) {
errno = EIO; \
perror(errmsg); \
if (file) fclose(file); \
- if (line) free(line); \
return 1; \
}
int main(int argc, char * argv[]) {
int ch;
- size_t lsize = 0;
- char * line = NULL;
+ char line[16*1024];
+ char *lineptr = NULL;
section_t section_list[] = {
{0x4000, false, "ROM0", false},
@@ -97,7 +96,7 @@ int main(int argc, char * argv[]) {
FILE * file = fopen(argv[arg_idx], "r");
if (file == NULL)
RIP("file io");
- while (getline(&line, &lsize, file) > 0) {
+ while ((lineptr = fgets(line, sizeof(line), file)) != NULL) {
unsigned short bank = 0;
unsigned short pointer = 0;
char * symbol = NULL;
@@ -105,28 +104,28 @@ int main(int argc, char * argv[]) {
char * addr_p;
// line = line.split(";")[0].strip()
- line += strspn(line, " \t\n");
- end = strchr(line, ';');
+ lineptr += strspn(lineptr, " \t\n");
+ end = strchr(lineptr, ';');
if (end) *end = 0;
- line[strrspn(line, " \t\n")] = 0;
+ lineptr[strrspn(lineptr, " \t\n")] = 0;
if (!*line)
continue;
// Get the bank, address, and symbol
- end = line + strcspn(line, " \t\n");
+ end = lineptr + strcspn(lineptr, " \t\n");
symbol = end + strspn(end, " \t\n");
if (!*symbol)
RIP("parse");
*end = 0;
- addr_p = strchr(line, ':');
+ addr_p = strchr(lineptr, ':');
if (!addr_p)
RIP("parse");
*addr_p++ = 0;
pointer = strtoul(addr_p, &end, 16);
if (pointer == 0 && end == addr_p)
RIP("parse");
- bank = strtoul(line, &end, 16);
- if (bank == 0 && end == line)
+ bank = strtoul(lineptr, &end, 16);
+ if (bank == 0 && end == lineptr)
RIP("parse");
// Main loop
@@ -158,6 +157,5 @@ int main(int argc, char * argv[]) {
}
fclose(file);
}
- free(line);
return 0;
}