From d0051dcf22aab73a7c3a116854212b8e8a5fc916 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sat, 15 Jul 2017 10:44:35 -0400 Subject: [PATCH] fixed all errors found by clang and fixed some compiler warnings as well as a use-after-free bug in exec.c --- config.mk | 2 +- inc/stdc.h | 2 +- inc/utf.h | 2 +- lib/buf.c | 17 ++--------------- lib/exec.c | 3 +-- lib/utf8.c | 12 ++++++------ lib/utils.c | 2 +- lib/view.c | 7 ++----- lib/win.c | 38 +++++++++++++++----------------------- pick.c | 2 ++ tide.c | 6 +++--- 11 files changed, 35 insertions(+), 58 deletions(-) diff --git a/config.mk b/config.mk index 8446221..f391e19 100644 --- a/config.mk +++ b/config.mk @@ -33,7 +33,7 @@ GCOV = 0 # Treat all warnings as errors (poor man's lint?) ifeq ($(WERROR), 1) - CFLAGS += -Wall -Wextra -Werror + CFLAGS += -Wall -Wextra -Wno-unused-parameter endif # GCC Debugging diff --git a/inc/stdc.h b/inc/stdc.h index cbbcd57..57d6596 100644 --- a/inc/stdc.h +++ b/inc/stdc.h @@ -44,7 +44,7 @@ typedef intptr_t intptr; /* Generic Death Function *****************************************************************************/ -void die(const char* msgfmt, ...); +void die(const char* msgfmt, ...) __attribute__((__noreturn__)); /* Option Parsing * diff --git a/inc/utf.h b/inc/utf.h index 0aa87f9..b057363 100644 --- a/inc/utf.h +++ b/inc/utf.h @@ -8,7 +8,7 @@ enum { }; /* Represents a unicode code point */ -typedef uint32_t Rune; +typedef int32_t Rune; size_t utf8encode(char str[UTF_MAX], Rune rune); bool utf8decode(Rune* rune, size_t* length, int byte); diff --git a/lib/buf.c b/lib/buf.c index 5c8bfb3..fc81b52 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -13,10 +13,8 @@ static void log_clear(Log** list); static void log_insert(Buf* buf, Log** list, size_t beg, size_t end); static void log_delete(Buf* buf, Log** list, size_t off, Rune* r, size_t len); static void syncgap(Buf* buf, size_t off); -static void buf_resize(Buf* buf, size_t sz); static void delete(Buf* buf, size_t off); static size_t insert(Buf* buf, size_t off, Rune rune); -static int range_match(Buf* buf, size_t dbeg, size_t dend, size_t mbeg, size_t mend); static int rune_match(Buf* buf, size_t mbeg, size_t mend, Rune* runes); static void swaplog(Buf* buf, Log** from, Log** to, Sel* sel); static size_t next_size(size_t curr); @@ -173,7 +171,6 @@ size_t buf_delete(Buf* buf, size_t beg, size_t end) { log_clear(&(buf->redo)); for (size_t i = end-beg; i > 0; i--) { Rune r = buf_get(buf, beg); - bool is_eol = (r == '\n' || r == RUNE_CRLF); log_delete(buf, &(buf->undo), beg, &r, 1); delete(buf, beg); } @@ -279,7 +276,7 @@ void buf_getword(Buf* buf, bool (*isword)(Rune), Sel* sel) { void buf_getblock(Buf* buf, Rune first, Rune last, Sel* sel) { int balance = 0, dir; - size_t beg = sel->end, end = sel->end, off; + size_t beg, end = sel->end; /* figure out which end of the block we're starting at */ if (buf_get(buf, end) == first) @@ -538,7 +535,7 @@ static void buf_resize(Buf* buf, size_t sz) { *(copy.gapstart++) = *(curr); /* free the buffer and commit the changes */ free(buf->bufstart); - *buf = copy; + memcpy(buf, ©, sizeof(Buf)); } static void delete(Buf* buf, size_t off) { @@ -566,16 +563,6 @@ static size_t insert(Buf* buf, size_t off, Rune rune) { return rcount; } -static int range_match(Buf* buf, size_t dbeg, size_t dend, size_t mbeg, size_t mend) { - size_t n1 = dend-dbeg, n2 = mend-mbeg; - if (n1 != n2) return n1-n2; - for (; n1 > 0; n1--, dbeg++, mbeg++) { - int cmp = buf_get(buf, dbeg) - buf_get(buf, mbeg); - if (cmp != 0) return cmp; - } - return 0; -} - static int rune_match(Buf* buf, size_t mbeg, size_t mend, Rune* runes) { for (; *runes; runes++, mbeg++) { int cmp = *runes - buf_get(buf, mbeg); diff --git a/lib/exec.c b/lib/exec.c index c264ea4..0c4d5bc 100644 --- a/lib/exec.c +++ b/lib/exec.c @@ -156,7 +156,7 @@ static void send_data(int fd, void* data) { job->nwrite += nwrite; } if (nwrite < 0 || job->ndata <= 0) - job_closefd(job, fd); + close(fd); } else { job_closefd(job, fd); } @@ -190,7 +190,6 @@ static void recv_data(int fd, void* data) { } } else { close(fd); - job_closefd(job, -fd); } } else { job_closefd(job, fd); diff --git a/lib/utf8.c b/lib/utf8.c index e4fddab..184340e 100644 --- a/lib/utf8.c +++ b/lib/utf8.c @@ -5,15 +5,15 @@ #include #include -const uint8_t UTF8_SeqBits[] = { 0x00u, 0x80u, 0xC0u, 0xE0u, 0xF0u, 0xF8u, 0xFCu, 0xFEu }; -const uint8_t UTF8_SeqMask[] = { 0x00u, 0xFFu, 0x1Fu, 0x0Fu, 0x07u, 0x03u, 0x01u, 0x00u }; -const uint8_t UTF8_SeqLens[] = { 0x01u, 0x00u, 0x02u, 0x03u, 0x04u, 0x05u, 0x06u, 0x00u }; +const uint8_t UTF8_SeqBits[] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; +const uint8_t UTF8_SeqMask[] = { 0x00, 0xFF, 0x1F, 0x0F, 0x07, 0x03, 0x01, 0x00 }; +const uint8_t UTF8_SeqLens[] = { 0x01, 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x00 }; static bool runevalid(Rune val) { return (val <= RUNE_MAX) - && ((val & 0xFFFEu) != 0xFFFEu) - && ((val < 0xD800u) || (val > 0xDFFFu)) - && ((val < 0xFDD0u) || (val > 0xFDEFu)); + && ((val & 0xFFFE) != 0xFFFE) + && ((val < 0xD800) || (val > 0xDFFF)) + && ((val < 0xFDD0) || (val > 0xFDEF)); } static size_t runelen(Rune rune) { diff --git a/lib/utils.c b/lib/utils.c index 8608da7..05e0b63 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -123,7 +123,7 @@ char* getcurrdir(void) { char* dirname(char* path) { path = stringdup(path); char* end = strrchr(path, '/'); - if (!end) return NULL; + if (!end) return (free(path), NULL); *end = '\0'; return path; } diff --git a/lib/view.c b/lib/view.c index 36a7b1b..90dbdbc 100644 --- a/lib/view.c +++ b/lib/view.c @@ -66,11 +66,10 @@ size_t view_limitrows(View* view, size_t maxrows, size_t ncols) { } void view_resize(View* view, size_t nrows, size_t ncols) { - size_t line = 1, off = 0; + size_t off = 0; if (view->nrows == nrows && view->ncols == ncols) return; /* free the old row data */ if (view->nrows) { - line = view->rows[0]->line; off = view->rows[0]->off; for (size_t i = 0; i < view->nrows; i++) free(view->rows[i]); @@ -701,10 +700,8 @@ static void apply_colors(View* view) { if (!curr) { r = -1; break; } // Break both loops if we're done /* check if we're in the current region */ - if (curr->beg <= off && off <= curr->end) { - uint32_t attr = row->cols[col].attr; + if (curr->beg <= off && off <= curr->end) row->cols[col].attr = (row->cols[col].attr & 0xFF00) | curr->color; - } off++, col++; while (col < row->len && row->cols[col].rune == '\0') col++; diff --git a/lib/win.c b/lib/win.c index c579d91..6f0c163 100644 --- a/lib/win.c +++ b/lib/win.c @@ -211,8 +211,6 @@ static void layout(int width, int height) { } static void onredraw(int width, int height) { - static uint64_t maxtime = 0; - uint64_t start = getmillis(); size_t fheight = x11_font_height(Font); size_t fwidth = x11_font_width(Font); @@ -286,10 +284,6 @@ static void onredraw(int width, int height) { size_t y = Regions[Focused].y + (Regions[Focused].csry * fheight) + (fheight/2); x11_mouse_set(x, y); } - - uint64_t stop = getmillis(); - uint64_t elapsed = stop-start; - //printf("%llu\n", elapsed); } static void oninput(int mods, Rune key) { @@ -326,7 +320,6 @@ static void oninput(int mods, Rune key) { static void scroll_actions(int btn, bool pressed, int x, int y) { size_t row = (y-Regions[SCROLL].y) / x11_font_height(Font); - size_t col = (x-Regions[SCROLL].x) / x11_font_width(Font); switch (btn) { case MouseLeft: if (pressed) @@ -405,24 +398,23 @@ static bool update_focus(void) { static void draw_line_num(bool current, size_t x, size_t y, size_t gcols, size_t num) { int color = config_get_int(ClrGutterNor); - if (ShowLineNumbers) { - if (current) { - color = config_get_int(ClrGutterSel);; - size_t fheight = x11_font_height(Font); - x11_draw_rect((color >> 8), x-3, y-fheight, gutter_size(), fheight); - } - UGlyph glyphs[gcols]; - for (int i = gcols-1; i >= 0; i--) { - glyphs[i].attr = color & 0xFF; - if (num > 0) { - glyphs[i].rune = ((num % 10) + '0'); - num /= 10; - } else { - glyphs[i].rune = ' '; - } + if (!gcols) return; + if (current) { + color = config_get_int(ClrGutterSel);; + size_t fheight = x11_font_height(Font); + x11_draw_rect((color >> 8), x-3, y-fheight, gutter_size(), fheight); + } + UGlyph glyphs[gcols]; + for (int i = gcols-1; i >= 0; i--) { + glyphs[i].attr = color & 0xFF; + if (num > 0) { + glyphs[i].rune = ((num % 10) + '0'); + num /= 10; + } else { + glyphs[i].rune = ' '; } - draw_glyphs(x, y, glyphs, gcols, gcols); } + draw_glyphs(x, y, glyphs, gcols, gcols); } static void draw_glyphs(size_t x, size_t y, UGlyph* glyphs, size_t rlen, size_t ncols) { diff --git a/pick.c b/pick.c index 84d0087..6d1ada5 100644 --- a/pick.c +++ b/pick.c @@ -61,6 +61,8 @@ static void load_choices(void) { choice.length = strlen(choice_text); choice.score = 1.0; vec_push_back(&Choices, &choice); + } else { + free(choice_text); } } vec_sort(&Choices, by_score); diff --git a/tide.c b/tide.c index 0f84018..3712d23 100644 --- a/tide.c +++ b/tide.c @@ -139,7 +139,7 @@ static void trim_whitespace(void) { static void quit(void) { static uint64_t before = 0; uint64_t now = getmillis(); - if (!win_buf(EDIT)->modified || (now-before) <= config_get_int(DblClickTime)) { + if (!win_buf(EDIT)->modified || (now-before) <= (uint64_t)config_get_int(DblClickTime)) { #ifndef TEST x11_deinit(); #else @@ -180,7 +180,7 @@ void onmouseleft(WinRegion id, bool pressed, size_t row, size_t col) { static uint64_t before = 0; if (!pressed) return; uint64_t now = getmillis(); - count = ((now-before) <= config_get_int(DblClickTime) ? count+1 : 1); + count = ((now-before) <= (uint64_t)config_get_int(DblClickTime) ? count+1 : 1); before = now; if (count == 1) { @@ -591,7 +591,7 @@ int pty_spawn(char** argv) { void pty_update(int fd, void* data) { /* Read from command if we have one */ - long n = 0, r = 0, i = 0; + long n = 0, i = 0; static char cmdbuf[8192]; if ((n = read(CmdFD, cmdbuf, sizeof(cmdbuf))) < 0) CmdFD = -1; -- 2.49.0