diff --git a/Makefile b/Makefile index 9672dff..0e00f2d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ CC=gcc CFLAGS=-Wall -Wextra -pedantic -Werror -OFLAGS=-O3 +#OFLAGS=-O3 +OFLAGS=-g LFLAGS=-lncursesw -ltcmalloc ste: ste.c diff --git a/hello b/hello index e4476b0..f4e5baf 100644 --- a/hello +++ b/hello @@ -4,3 +4,5 @@ hello hello hello hello + +sdsds \ No newline at end of file diff --git a/ste b/ste index ed0cf4d..681b12b 100755 Binary files a/ste and b/ste differ diff --git a/ste.c b/ste.c index b6ddaaa..9720459 100644 --- a/ste.c +++ b/ste.c @@ -51,7 +51,7 @@ typedef struct row { * defines rows and teh number of rows */ struct { row *rw; - int rownum; + long rownum; } rows; /* Prototypes */ @@ -63,7 +63,7 @@ static void updateRender (row *rw); static void updateScroll (void); static void cursorMove(int a); static int decimalSize (int n); -static void lnMove (int y, int x); +static inline void lnMove (int y, int x); static int curRealToRender (row *rw, int c_x); /* Row operations */ @@ -110,7 +110,7 @@ int main (int argc, char *argv[]) termInit(); /* Set the statusbar left (static) message */ - sprintf(t.statusbar, "%s : %s %d lines %dx%d", argv[0], argv[1], rows.rownum, t.dim.y, t.dim.y); + sprintf(t.statusbar, "%s : %s %ld lines %dx%d", argv[0], argv[1], rows.rownum, t.dim.y, t.dim.y); /* remember the initial row number */ int irow = decimalSize(rows.rownum); @@ -249,28 +249,24 @@ void drawScreen () /* Draw all the appropriate lines (following cursor) to the screen */ void drawLines (void) { - int line = 0, ln; + static unsigned int line = 0, ln = 0; + static int i = 0; /* move to the beginning of the screen */ - lnMove(0, 0); + //lnMove(0, 0); - for (int i = 0; i < t.dim.y; i++) { + for (i = 0; i < t.dim.y; i++) { if (i >= rows.rownum) break; ln = i + t.cur.off_y; /* Draw the line number */ attron(COLOR_PAIR(1)); - mvprintw(i, 0, "%d", ln + 1); + move(i, 0); + printw("%d", ln + 1); attroff(COLOR_PAIR(1)); lnMove(i, 0); - //if (ln == t.cur.y + t.cur.off_y) attron(COLOR_PAIR(2)); - /* Draw the line matcing render memory */ - if (rows.rw[ln].r_size >= t.cur.off_x) { - addnstr(&rows.rw[ln].render[t.cur.off_x], t.dim.x + 1 - rows.rw[ln].delta); - } - - //attroff(COLOR_PAIR(2)); + addnstr(&rows.rw[ln].render[t.cur.off_x], t.dim.x + 1 - rows.rw[ln].delta); lnMove(++line, 0); } @@ -280,8 +276,7 @@ void drawLines (void) /* Move avoiding the space allocated for line numbers */ void lnMove (int y, int x) { - x += t.pad; - move(y, x); + move(y, x + t.pad); } /* Draw the status bar at the bottom of the screen */ @@ -297,7 +292,7 @@ void drawBar (char *s) for (int i = len; i <= t.dim.x + t.pad; i++) mvaddch(t.dim.y, i, ' '); - char m[40]; + static char m[40]; sprintf(m, "x: %d y: %d Zoom: %c", t.cur.xx, t.cur.yy, whatsThat()); mvaddstr(t.dim.y, t.dim.x + t.pad - strlen(m), m); @@ -308,8 +303,8 @@ void drawBar (char *s) /* convert the cursor matchoing the memory to the drawn one */ int curRealToRender (row *rw, int c_x) { - int r_x = 0; - for (int i = 0; i < c_x; i++) { + static int r_x = 0, i = 0; + for (i = 0, r_x = 0; i < c_x; i++) { if (rw->chars[i] == '\t') r_x += (TABSIZE - 1) - (r_x % TABSIZE); r_x++; } @@ -347,11 +342,16 @@ void fileOpen (char *filename) void rowAddLast (char *s, int len) { /* Extend the block of memory containing the lines */ - rows.rw = realloc(rows.rw, sizeof(row) * (rows.rownum + 1)); + // reallocarray fails safely + row *newr = NULL; + newr = (row*) reallocarray(rows.rw, (rows.rownum + 1), sizeof(*newr)); + if (newr == NULL) termDie("realloc rowAddLast"); + else rows.rw = newr; + //rows.rw = (row*) realloc(rows.rw, sizeof(row) * (rows.rownum + 1)); /* Allocate memory for the line and copy it * at the current row number */ - rows.rw[rows.rownum].chars = malloc(len + 1); + rows.rw[rows.rownum].chars = (char*) malloc(len + 1); memcpy(rows.rw[rows.rownum].chars, s, len); rows.rw[rows.rownum].chars[len] = '\0'; rows.rw[rows.rownum].size = len; @@ -363,7 +363,7 @@ void updateRender (row *rw) { /* count the special characters (only tabs for now) */ int tabs = 0, i; - for (i = 0; i < rw->size; i++) { + for (i = 0; i <= rw->size; i++) { if (rw->chars[i] == '\t') tabs++; } rw->render = NULL; @@ -377,10 +377,11 @@ void updateRender (row *rw) /* put all the characters (substituing all special chars) * into the render buffer */ - int off = 0; - for (i = 0; i < rw->size; i++) { + static int off, j = 0; + off = 0; + for (i = 0; i <= rw->size; i++) { if (rw->chars[i] == '\t') { - for (int j = 0; j < TABSIZE; j++){ + for (j = 0; j < TABSIZE; j++){ if (!j) rw->render[off++] = '|'; else rw->render[off++] = ' '; } @@ -388,6 +389,7 @@ void updateRender (row *rw) rw->render[off++] = rw->chars[i]; } } + off -= 1; rw->render[off] = '\0'; rw->r_size = off; } @@ -407,7 +409,7 @@ void rowAddChar (row *rw, char c) // WIP char *s = rw->chars; // reallocate mem and inc size - rw->chars = malloc(rw->size + 2); + rw->chars = (char*) malloc(rw->size + 2); rw->size++; // copy bf cursor @@ -623,6 +625,7 @@ void rowCpy (row *to, row *from) // WIP { rowFree(to); to->chars = malloc(strlen(from->chars) + 1); + if (to->chars == NULL) termDie("malloc in rowCpy"); to->size = from->size; memcpy(to->chars, from->chars, to->size); updateRender(to); diff --git a/valog.txt b/valog.txt new file mode 100644 index 0000000..f23650f --- /dev/null +++ b/valog.txt @@ -0,0 +1,167 @@ +==27775== Memcheck, a memory error detector +==27775== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. +==27775== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info +==27775== Command: ./ste ste.c +==27775== Parent PID: 25299 +==27775== +==27775== Mismatched free() / delete / delete [] +==27775== at 0x48379AB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x48D8824: ??? (in /usr/lib/libtcmalloc.so.4.5.3) +==27775== by 0x400F219: call_init.part.0 (in /usr/lib/ld-2.30.so) +==27775== by 0x400F318: _dl_init (in /usr/lib/ld-2.30.so) +==27775== by 0x40010C9: ??? (in /usr/lib/ld-2.30.so) +==27775== by 0x1: ??? +==27775== by 0x1FFF0008E6: ??? +==27775== by 0x1FFF0008EC: ??? +==27775== Address 0x52ccd20 is 0 bytes inside a block of size 4 alloc'd +==27775== at 0x483750F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x48D8817: ??? (in /usr/lib/libtcmalloc.so.4.5.3) +==27775== by 0x400F219: call_init.part.0 (in /usr/lib/ld-2.30.so) +==27775== by 0x400F318: _dl_init (in /usr/lib/ld-2.30.so) +==27775== by 0x40010C9: ??? (in /usr/lib/ld-2.30.so) +==27775== by 0x1: ??? +==27775== by 0x1FFF0008E6: ??? +==27775== by 0x1FFF0008EC: ??? +==27775== +==27775== Mismatched free() / delete / delete [] +==27775== at 0x48379AB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x4E2B94E: std::__cxx11::basic_string, std::allocator >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) (in /usr/lib/libstdc++.so.6.0.27) +==27775== by 0x4E2CEEA: std::__cxx11::basic_string, std::allocator >::_M_append(char const*, unsigned long) (in /usr/lib/libstdc++.so.6.0.27) +==27775== by 0x48EE629: MallocExtension::Initialize() (in /usr/lib/libtcmalloc.so.4.5.3) +==27775== by 0x48D8829: ??? (in /usr/lib/libtcmalloc.so.4.5.3) +==27775== by 0x400F219: call_init.part.0 (in /usr/lib/ld-2.30.so) +==27775== by 0x400F318: _dl_init (in /usr/lib/ld-2.30.so) +==27775== by 0x40010C9: ??? (in /usr/lib/ld-2.30.so) +==27775== by 0x1: ??? +==27775== by 0x1FFF0008E6: ??? +==27775== by 0x1FFF0008EC: ??? +==27775== Address 0x52cd140 is 0 bytes inside a block of size 23 alloc'd +==27775== at 0x483750F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x48EE5C7: MallocExtension::Initialize() (in /usr/lib/libtcmalloc.so.4.5.3) +==27775== by 0x48D8829: ??? (in /usr/lib/libtcmalloc.so.4.5.3) +==27775== by 0x400F219: call_init.part.0 (in /usr/lib/ld-2.30.so) +==27775== by 0x400F318: _dl_init (in /usr/lib/ld-2.30.so) +==27775== by 0x40010C9: ??? (in /usr/lib/ld-2.30.so) +==27775== by 0x1: ??? +==27775== by 0x1FFF0008E6: ??? +==27775== by 0x1FFF0008EC: ??? +==27775== +==27775== Mismatched free() / delete / delete [] +==27775== at 0x48379AB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x48EE63C: MallocExtension::Initialize() (in /usr/lib/libtcmalloc.so.4.5.3) +==27775== by 0x48D8829: ??? (in /usr/lib/libtcmalloc.so.4.5.3) +==27775== by 0x400F219: call_init.part.0 (in /usr/lib/ld-2.30.so) +==27775== by 0x400F318: _dl_init (in /usr/lib/ld-2.30.so) +==27775== by 0x40010C9: ??? (in /usr/lib/ld-2.30.so) +==27775== by 0x1: ??? +==27775== by 0x1FFF0008E6: ??? +==27775== by 0x1FFF0008EC: ??? +==27775== Address 0x52cd1a0 is 0 bytes inside a block of size 45 alloc'd +==27775== at 0x483750F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x4E2B8CC: std::__cxx11::basic_string, std::allocator >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) (in /usr/lib/libstdc++.so.6.0.27) +==27775== by 0x4E2CEEA: std::__cxx11::basic_string, std::allocator >::_M_append(char const*, unsigned long) (in /usr/lib/libstdc++.so.6.0.27) +==27775== by 0x48EE629: MallocExtension::Initialize() (in /usr/lib/libtcmalloc.so.4.5.3) +==27775== by 0x48D8829: ??? (in /usr/lib/libtcmalloc.so.4.5.3) +==27775== by 0x400F219: call_init.part.0 (in /usr/lib/ld-2.30.so) +==27775== by 0x400F318: _dl_init (in /usr/lib/ld-2.30.so) +==27775== by 0x40010C9: ??? (in /usr/lib/ld-2.30.so) +==27775== by 0x1: ??? +==27775== by 0x1FFF0008E6: ??? +==27775== by 0x1FFF0008EC: ??? +==27775== +==27775== Conditional jump or move depends on uninitialised value(s) +==27775== at 0x486C3E4: waddnstr (in /usr/lib/libncursesw.so.6.1) +==27775== by 0x109899: drawLines (ste.c:269) +==27775== by 0x109739: drawScreen (ste.c:240) +==27775== by 0x1093C9: main (ste.c:122) +==27775== +==27775== Conditional jump or move depends on uninitialised value(s) +==27775== at 0x486C3F2: waddnstr (in /usr/lib/libncursesw.so.6.1) +==27775== by 0x109899: drawLines (ste.c:269) +==27775== by 0x109739: drawScreen (ste.c:240) +==27775== by 0x1093C9: main (ste.c:122) +==27775== +==27775== Conditional jump or move depends on uninitialised value(s) +==27775== at 0x486C41B: waddnstr (in /usr/lib/libncursesw.so.6.1) +==27775== by 0x109899: drawLines (ste.c:269) +==27775== by 0x109739: drawScreen (ste.c:240) +==27775== by 0x1093C9: main (ste.c:122) +==27775== +==27775== Invalid read of size 4 +==27775== at 0x10A794: rowAddRow (ste.c:580) +==27775== by 0x109521: main (ste.c:144) +==27775== Address 0x5ae6ef0 is 0 bytes after a block of size 25,760 alloc'd +==27775== at 0x4838D7B: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x109C39: rowAddLast (ste.c:347) +==27775== by 0x109BAB: fileOpen (ste.c:333) +==27775== by 0x109357: main (ste.c:106) +==27775== +==27775== Invalid read of size 8 +==27775== at 0x10A7B8: rowAddRow (ste.c:580) +==27775== by 0x109521: main (ste.c:144) +==27775== Address 0x5ae6ef8 is 8 bytes after a block of size 25,760 alloc'd +==27775== at 0x4838D7B: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x109C39: rowAddLast (ste.c:347) +==27775== by 0x109BAB: fileOpen (ste.c:333) +==27775== by 0x109357: main (ste.c:106) +==27775== +==27775== Conditional jump or move depends on uninitialised value(s) +==27775== at 0x109D9E: updateRender (ste.c:367) +==27775== by 0x10AC66: rowCpy (ste.c:631) +==27775== by 0x10A81F: rowAddRow (ste.c:583) +==27775== by 0x109521: main (ste.c:144) +==27775== Uninitialised value was created by a heap allocation +==27775== at 0x483677F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x10AC06: rowCpy (ste.c:627) +==27775== by 0x10A81F: rowAddRow (ste.c:583) +==27775== by 0x109521: main (ste.c:144) +==27775== +==27775== Conditional jump or move depends on uninitialised value(s) +==27775== at 0x109E3C: updateRender (ste.c:383) +==27775== by 0x10AC66: rowCpy (ste.c:631) +==27775== by 0x10A81F: rowAddRow (ste.c:583) +==27775== by 0x109521: main (ste.c:144) +==27775== Uninitialised value was created by a heap allocation +==27775== at 0x483677F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x10AC06: rowCpy (ste.c:627) +==27775== by 0x10A81F: rowAddRow (ste.c:583) +==27775== by 0x109521: main (ste.c:144) +==27775== +==27775== Conditional jump or move depends on uninitialised value(s) +==27775== at 0x4839C78: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x10ABFA: rowCpy (ste.c:627) +==27775== by 0x10A81F: rowAddRow (ste.c:583) +==27775== by 0x109521: main (ste.c:144) +==27775== Uninitialised value was created by a heap allocation +==27775== at 0x483677F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x10AC06: rowCpy (ste.c:627) +==27775== by 0x10A81F: rowAddRow (ste.c:583) +==27775== by 0x109521: main (ste.c:144) +==27775== +==27775== Conditional jump or move depends on uninitialised value(s) +==27775== at 0x4839C65: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x10ABFA: rowCpy (ste.c:627) +==27775== by 0x10A81F: rowAddRow (ste.c:583) +==27775== by 0x109521: main (ste.c:144) +==27775== Uninitialised value was created by a heap allocation +==27775== at 0x483677F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) +==27775== by 0x10AC06: rowCpy (ste.c:627) +==27775== by 0x10A81F: rowAddRow (ste.c:583) +==27775== by 0x109521: main (ste.c:144) +==27775== +==27775== +==27775== HEAP SUMMARY: +==27775== in use at exit: 334,484 bytes in 1,653 blocks +==27775== total heap usage: 30,194 allocs, 28,541 frees, 9,926,406 bytes allocated +==27775== +==27775== LEAK SUMMARY: +==27775== definitely lost: 0 bytes in 0 blocks +==27775== indirectly lost: 0 bytes in 0 blocks +==27775== possibly lost: 0 bytes in 0 blocks +==27775== still reachable: 334,484 bytes in 1,653 blocks +==27775== suppressed: 0 bytes in 0 blocks +==27775== Reachable blocks (those to which a pointer was found) are not shown. +==27775== To see them, rerun with: --leak-check=full --show-leak-kinds=all +==27775== +==27775== For lists of detected and suppressed errors, rerun with: -s +==27775== ERROR SUMMARY: 65744 errors from 12 contexts (suppressed: 0 from 0)