added checks on all mallocs

master
ElectricAlchemist 5 years ago
parent 6eb557fc48
commit 2bd14339aa
  1. 15
      ste.c

15
ste.c

@ -44,7 +44,6 @@ typedef struct row {
char *chars; char *chars;
unsigned int r_size; unsigned int r_size;
char *render; char *render;
int delta;
} row; } row;
/* Rows structure (or file buffer) /* Rows structure (or file buffer)
@ -251,7 +250,6 @@ void drawLines (void)
{ {
static int ln, i; static int ln, i;
/* move to the beginning of the screen */ /* move to the beginning of the screen */
//lnMove(0, 0);
for (i = 0, ln = 0; i < t.dim.y; i++) { for (i = 0, ln = 0; i < t.dim.y; i++) {
ln = i + t.cur.off_y; ln = i + t.cur.off_y;
@ -263,14 +261,10 @@ void drawLines (void)
attroff(COLOR_PAIR(1)); attroff(COLOR_PAIR(1));
lnMove(i, 0); lnMove(i, 0);
//if (ln == t.cur.y + t.cur.off_y) attron(COLOR_PAIR(2));
/* Draw the line matcing render memory */ /* Draw the line matcing render memory */
if (rows.rw[ln].r_size > t.cur.off_x) { if (rows.rw[ln].r_size > t.cur.off_x) {
addnstr(&rows.rw[ln].render[t.cur.off_x], t.dim.x + 1); addnstr(&rows.rw[ln].render[t.cur.off_x], t.dim.x + 1);
} }
//attroff(COLOR_PAIR(2));
lnMove(i + 1, 0); lnMove(i + 1, 0);
} }
@ -354,6 +348,7 @@ void rowAddLast (char *s, int len)
/* Allocate memory for the line and copy it /* Allocate memory for the line and copy it
* at the current row number */ * at the current row number */
rows.rw[rows.rownum].chars = malloc(len + 1); rows.rw[rows.rownum].chars = malloc(len + 1);
if (rows.rw[rows.rownum].chars == NULL) termDie("malloc in rowAddLast chars");
memcpy(rows.rw[rows.rownum].chars, s, len); memcpy(rows.rw[rows.rownum].chars, s, len);
rows.rw[rows.rownum].chars[len] = '\0'; rows.rw[rows.rownum].chars[len] = '\0';
rows.rw[rows.rownum].size = len; rows.rw[rows.rownum].size = len;
@ -374,7 +369,6 @@ void updateRender (row *rw)
/* Render is long as size with the added tab spaces - 1 /* Render is long as size with the added tab spaces - 1
* (we already count for the \t as a char) */ * (we already count for the \t as a char) */
rw->render = malloc(rw->size + tabs * (TABSIZE - 1) + 1); rw->render = malloc(rw->size + tabs * (TABSIZE - 1) + 1);
if (rw->render == NULL) termDie ("malloc in updateRender"); if (rw->render == NULL) termDie ("malloc in updateRender");
/* put all the characters (substituing all special chars) /* put all the characters (substituing all special chars)
@ -409,6 +403,7 @@ void rowAddChar (row *rw, char c) // WIP
// reallocate mem and inc size // reallocate mem and inc size
rw->chars = malloc(rw->size + 2); rw->chars = malloc(rw->size + 2);
if (rw->chars == NULL) termDie("malloc in rowAddchar");
rw->size++; rw->size++;
// copy bf cursor // copy bf cursor
@ -439,6 +434,7 @@ void rowDeleteChar (row *rw, int m) // WIP
if (s[t.cur.xx] == '\0' && m) return; if (s[t.cur.xx] == '\0' && m) return;
rw->chars = malloc(rw->size); rw->chars = malloc(rw->size);
if (rw->chars == NULL) termDie("malloc in rowDeleteChar");
rw->size--; rw->size--;
// Backspace // Backspace
@ -585,14 +581,17 @@ void rowAddRow (int pos) // WIP; TO DOCUMENT
//copy previous row //copy previous row
int l = rows.rw[pos].size - t.cur.xx; int l = rows.rw[pos].size - t.cur.xx;
s = malloc(l + 1); s = malloc(l + 1);
if (s == NULL) termDie("malloc in rowAddRow s");
memcpy(s, &rows.rw[pos].chars[t.cur.xx], l); memcpy(s, &rows.rw[pos].chars[t.cur.xx], l);
s[l] = '\0'; s[l] = '\0';
// Delete prev row until cursor // Delete prev row until cursor
char *p = malloc(t.cur.xx + 1); char *p = malloc(t.cur.xx + 1);
if (p == NULL) termDie("malloc in rowAddRow p");
memcpy(p, rows.rw[pos].chars, t.cur.xx); memcpy(p, rows.rw[pos].chars, t.cur.xx);
p[t.cur.xx] = '\0'; p[t.cur.xx] = '\0';
rowFree(&rows.rw[pos]); rowFree(&rows.rw[pos]);
rows.rw[pos].chars = malloc(t.cur.xx + 1); rows.rw[pos].chars = malloc(t.cur.xx + 1);
if (rows.rw[pos].chars == NULL) termDie("malloc in rowAddRow chars until cursor");
memcpy(rows.rw[pos].chars, p, t.cur.xx + 1); memcpy(rows.rw[pos].chars, p, t.cur.xx + 1);
free(p); free(p);
rows.rw[pos].size = t.cur.xx; rows.rw[pos].size = t.cur.xx;
@ -601,6 +600,7 @@ void rowAddRow (int pos) // WIP; TO DOCUMENT
if (pos != rows.rownum - 1) { if (pos != rows.rownum - 1) {
rowFree(&rows.rw[pos + 1]); rowFree(&rows.rw[pos + 1]);
rows.rw[pos + 1].chars = malloc(strlen(s) + 1); rows.rw[pos + 1].chars = malloc(strlen(s) + 1);
if (rows.rw[pos + 1].chars == NULL) termDie("malloc in rowAddRow chars next row");
memcpy(rows.rw[pos + 1].chars, s, strlen(s) + 1); memcpy(rows.rw[pos + 1].chars, s, strlen(s) + 1);
rows.rw[pos + 1].size = strlen(s); rows.rw[pos + 1].size = strlen(s);
updateRender(&rows.rw[pos + 1]); updateRender(&rows.rw[pos + 1]);
@ -624,6 +624,7 @@ void rowCpy (row *to, row *from) // WIP
{ {
rowFree(to); rowFree(to);
to->chars = (char*) malloc(strlen(from->chars) + 1); to->chars = (char*) malloc(strlen(from->chars) + 1);
if (to->chars == NULL) termDie("malloc in rowCpy");
to->size = from->size; to->size = from->size;
memcpy(to->chars, from->chars, to->size); memcpy(to->chars, from->chars, to->size);
updateRender(to); updateRender(to);

Loading…
Cancel
Save