Fix memory leak and NULL-check error

This commit is contained in:
Shav Kinderlehrer 2023-05-01 18:07:53 -04:00
parent 36af47eab8
commit 1c90bf55b1
6 changed files with 19 additions and 10 deletions

1
.gitignore vendored
View File

@ -131,3 +131,4 @@ $RECYCLE.BIN/
# End of https://www.toptal.com/developers/gitignore/api/c,macos,windows,linux,c # End of https://www.toptal.com/developers/gitignore/api/c,macos,windows,linux,c
build/

View File

@ -6,7 +6,11 @@ ODIR:=obj
BINDIR:=build BINDIR:=build
CC:=cc CC:=cc
CFLAGS:=-I$(IDIR) -Wall -Wextra -pedantic -Ofast # OMG SO FAST (see https://www.shlomifish.org/humour/by-others/funroll-loops/Gentoo-is-Rice.html)
# CFLAGS:=-I$(IDIR) -Wall -Wextra -pedantic -Ofast -faggressive-loop-optimizations -funroll-all-loops -march=native
# For a stable experience
CFLAGS:=-I$(IDIR) -Wall -Wextra -pedantic -O2 -march=native
LIB:= LIB:=
DEPS:=$($(IDIR)/%.h) DEPS:=$($(IDIR)/%.h)

View File

@ -2,7 +2,7 @@
#define ARG_H #define ARG_H
#include <stdbool.h> #include <stdbool.h>
#define LAT_VERSION "0.12.1" #define LAT_VERSION "0.12.2"
struct config { struct config {
bool stdin; bool stdin;

View File

@ -42,7 +42,6 @@ struct filedata readfile(FILE *fp, bool isstdin) {
while (fread(&c, 1, 1, fp) > 0) { while (fread(&c, 1, 1, fp) > 0) {
if (f.buflen == bufsize - 1) { if (f.buflen == bufsize - 1) {
bufsize *= 2; bufsize *= 2;
char *new_buf = realloc(f.buf, bufsize); char *new_buf = realloc(f.buf, bufsize);
if (new_buf == NULL) if (new_buf == NULL)
die("realloc"); die("realloc");

View File

@ -1,11 +1,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#include "types.h" #include "types.h"
#include "util.h" #include "util.h"
void appendline(struct filedata *f, char *data, size_t len) { void appendline(struct filedata *f, char *data, size_t len) {
f->lines = realloc(f->lines, sizeof(struct line) * (f->lc + 1)); f->lines = realloc(f->lines, sizeof(struct line) * (f->lc + 1));
if (f->lines == NULL)
die("realloc");
size_t loc = f->lc; size_t loc = f->lc;
@ -19,7 +23,7 @@ void loadlines(struct filedata *f) {
f->lc = 0; f->lc = 0;
size_t offset = 0; size_t offset = 0;
size_t linelen = 4096; size_t linelen = 1024;
char *line = malloc(linelen); char *line = malloc(linelen);
if (line == NULL) if (line == NULL)
die("malloc"); die("malloc");
@ -27,10 +31,11 @@ void loadlines(struct filedata *f) {
for (size_t i = 0; i < f->buflen; i++) { for (size_t i = 0; i < f->buflen; i++) {
char c = f->buf[i]; char c = f->buf[i];
if (c == '\n') { if (c == '\n') {
if (offset < linelen) { // shrink if (offset >= 1 && offset < linelen) { // shrink to fit
char *new_line = realloc(line, offset); char *new_line = realloc(line, offset);
if (new_line == NULL) if (new_line == NULL)
die("realloc"); die("realloc");
line = new_line; line = new_line;
} }
@ -58,7 +63,7 @@ void loadlines(struct filedata *f) {
} }
// capture last line // capture last line
if (offset < linelen) { // shrink if (offset >= 1 && offset < linelen) { // shrink
char *new_line = realloc(line, offset); char *new_line = realloc(line, offset);
if (new_line == NULL) if (new_line == NULL)
die("realloc"); die("realloc");

View File

@ -1,12 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "arg.h" #include "arg.h"
#include "file.h" #include "file.h"
#include "process.h" #include "process.h"
#include "types.h" #include "types.h"
#include "util.h" #include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define INVERT_T "\x1b[7m" #define INVERT_T "\x1b[7m"
#define UINVERT_T "\x1b[27m" #define UINVERT_T "\x1b[27m"
@ -77,6 +76,7 @@ void run(FILE *fp, char *filename, bool tty) {
fwrite("\n", 1, 1, err); fwrite("\n", 1, 1, err);
} }
free(f.buf); free(f.buf);
free(f.lines);
fflush(st); // prevent timing inconsistencies between st and err fflush(st); // prevent timing inconsistencies between st and err
fflush(err); fflush(err);