AmendHub

Download:

jcs

/

amend

/

amendments

/

110

*: Lots of little fixes and dead variable removal

Found by cppcheck and scan-build

jcs made amendment 110 about 1 year ago
--- bile.c Thu Nov 10 10:32:52 2022 +++ bile.c Sun Feb 5 09:37:19 2023 @@ -126,10 +126,9 @@ struct bile * bile_open(const Str255 filename, short vrefnum) { struct bile *bile = NULL; - struct bile_object map_obj, *o; char magic[BILE_MAGIC_LEN + 1]; - size_t file_size, map_size, size; - short fh, old_map_tried = 0; + size_t file_size, size; + short fh; _bile_error = 0; @@ -193,8 +192,7 @@ bile_open(const Str255 filename, short vrefnum) return bile; open_bail: - FSClose(bile->frefnum); - bile->frefnum = -1; + FSClose(fh); if (bile != NULL) xfree(&bile); return NULL; @@ -222,13 +220,14 @@ bile_open_recover_map(const Str255 filename, short vre if (bile) { bile->map_ptr = bile->old_map_ptr; bile_write_map(bile); + bile_flush(bile, true); } return bile; } short -bile_flush(struct bile *bile, short and_vol) +bile_flush(struct bile *bile, bool and_vol) { IOParam pb; short ret; @@ -270,7 +269,6 @@ struct bile_object * bile_find(struct bile *bile, const OSType type, const unsigned long id) { struct bile_object *o, *ocopy; - unsigned long n; char note[MALLOC_NOTE_SIZE]; bile_check_sanity(bile); @@ -470,7 +468,7 @@ bile_read_object(struct bile *bile, const struct bile_ void *data, const size_t len) { struct bile_object verify; - size_t rsize, wantlen, ret; + size_t rsize, wantlen; bile_check_sanity(bile); @@ -552,7 +550,6 @@ bile_read(struct bile *bile, const OSType type, const void *data, const size_t len) { struct bile_object *o; - size_t ret; bile_check_sanity(bile); @@ -608,7 +605,6 @@ bile_write(struct bile *bile, const OSType type, const { struct bile_object *old, *new_obj; size_t wrote; - short error; bile_check_sanity(bile); @@ -767,7 +763,6 @@ bile_unmarshall_object(struct bile *bile, short bile_verify(struct bile *bile) { - struct bile_object o; size_t n, size, pos; char data; @@ -817,7 +812,6 @@ struct bile_object * bile_alloc(struct bile *bile, const OSType type, const unsigned long id, const size_t size) { - struct bile_object newo; size_t last_pos = BILE_HEADER_LEN; size_t n, map_pos; @@ -967,6 +961,9 @@ bile_write_map(struct bile *bile) new_map[new_nobjects++] = *obj; } + if (new_map_obj_in_new_map == NULL) + panic("bile_write_map: no new map object in new map"); + /* shrink to new object count */ new_map_size = BILE_OBJECT_SIZE * new_nobjects; new_map_obj->size = new_map_size; @@ -1024,9 +1021,7 @@ size_t bile_xwriteat(struct bile *bile, const size_t pos, const void *data, const size_t len) { - short error; size_t wsize, tsize; - long asize; bile_check_sanity(bile); --- bile.h Thu Nov 10 10:33:26 2022 +++ bile.h Mon Feb 6 10:33:53 2023 @@ -106,7 +106,7 @@ struct bile * bile_create(const Str255 filename, sho struct bile * bile_open(const Str255 filename, short vrefnum); struct bile * bile_open_recover_map(const Str255 filename, short vrefnum); -short bile_flush(struct bile *bile, short and_vol); +short bile_flush(struct bile *bile, bool and_vol); void bile_close(struct bile *bile); struct bile_object * bile_find(struct bile *bile, const OSType type, --- browser.c Thu Nov 10 09:28:59 2022 +++ browser.c Mon Feb 6 10:22:39 2023 @@ -115,7 +115,7 @@ browser_init(struct repo *repo) Rect data_bounds = { 0, 0, 0, 1 }; /* tlbr */ Point cell_size = { 0, 0 }; Cell cell = { 0 }; - short colonpos, n, width, height; + short width, height; browser = xmalloczero(sizeof(struct browser), "browser"); browser->state = BROWSER_STATE_IDLE; @@ -351,7 +351,7 @@ browser_filter_amendments(struct browser *browser) { Cell cell = { 0 }; struct repo_amendment *amendment; - short i, j, k, add = 0, file_id; + short i, j, k, add = 0; short *selected_files = NULL; short nselected_files = 0; @@ -366,7 +366,6 @@ browser_filter_amendments(struct browser *browser) add = 0; amendment = browser->repo->amendments[i]; for (j = 0; j < amendment->nfiles; j++) { - file_id = amendment->file_ids[j]; for (k = 0; k < nselected_files; k++) { if (selected_files[k] == amendment->file_ids[j]) { add = 1; @@ -397,8 +396,6 @@ void browser_show_amendment(struct browser *browser, struct repo_amendment *amendment) { - struct repo_diff *diff; - if (amendment == NULL) { TESetText("", 0, browser->diff_te); HLock(browser->diff_te); @@ -450,7 +447,7 @@ browser_export_amendment(struct browser *browser) { Cell selected = { 0 }; struct repo_amendment *amendment; - short error, len; + short len; SFReply reply; char filename[255]; @@ -488,11 +485,9 @@ browser_apply_diff(struct browser *browser) void browser_edit_amendment(struct browser *browser) { - Point pt = { 75, 100 }; Cell selected = { 0 }; struct repo_amendment *amendment; - short error, len; - SFReply reply; + short len; if (LGetSelect(true, &selected, browser->amendment_list) == false) return; @@ -506,7 +501,6 @@ browser_edit_amendment(struct browser *browser) void browser_update_menu(struct browser *browser) { - size_t vlines; TERec *diff; Cell cell = { 0, 0 }; @@ -558,7 +552,6 @@ void browser_update(struct focusable *focusable, EventRecord *event) { struct browser *browser = (struct browser *)focusable->cookie; - Str255 buf; Rect r; short what = -1; @@ -611,8 +604,7 @@ browser_mouse_down(struct focusable *focusable, EventR struct repo_amendment *amendment = NULL; short *selected_files = NULL, *now_selected_files = NULL; short nselected = 0, nnow_selected = 0; - short val, adj, page, was_selected, part, i, data_len; - char *path; + short val, adj, was_selected, i, data_len; p = event->where; GlobalToLocal(&p); @@ -697,7 +689,7 @@ browser_mouse_down(struct focusable *focusable, EventR return; } - switch (part = FindControl(p, browser->win, &control)) { + switch (FindControl(p, browser->win, &control)) { case inButton: TextFont(DIFF_BUTTON_FONT); TextSize(DIFF_BUTTON_FONT_SIZE); --- committer.c Thu Nov 10 09:31:29 2022 +++ committer.c Mon Feb 6 10:24:29 2023 @@ -78,7 +78,7 @@ committer_init(struct browser *browser) bounds.right -= (PADDING / 2); bounds.bottom -= (PADDING / 2); - memcpy(filename, browser->repo->bile->filename, sizeof(filename)); + memcpy(&filename, browser->repo->bile->filename, sizeof(filename)); PtoCstr(filename); snprintf((char *)&title, sizeof(title), "%s: %s: diff", PROGRAM_NAME, (browser->repo ? (char *)filename : "No repo open")); @@ -307,7 +307,7 @@ committer_mouse_down(struct focusable *focusable, Even Point p; ControlHandle control; Rect r; - short val, adj, page, was_selected, part, i; + short val, adj; p = event->where; GlobalToLocal(&p); @@ -329,7 +329,7 @@ committer_mouse_down(struct focusable *focusable, Even return; } - switch (part = FindControl(p, committer->win, &control)) { + switch (FindControl(p, committer->win, &control)) { case inButton: if (TrackControl(control, p, 0L) && control == committer->commit_button) @@ -425,7 +425,6 @@ committer_generate_diff(struct committer *committer) short i, all_files; short *selected_files = NULL; short nselected_files = 0; - short diff_text = 0; TextStyle style; SetCursor(*(GetCursor(watchCursor))); @@ -631,8 +630,6 @@ diff_output(const char *format, ...) void diff_append_line(char *str, size_t len, bool flush) { - short tabsize; - if (committer_diffing == NULL) panic("diff_append_line without committer_diffing"); --- commit_list.c Wed Aug 31 16:28:54 2022 +++ commit_list.c Mon Feb 6 10:23:02 2023 @@ -31,8 +31,6 @@ pascal void amendment_list_ldef(short message, Boolean selected, Rect *cellRect, Cell theCell, short dataOffset, short dataLen, ListHandle theList) { - short id; - switch (message) { case 0: /* init */ @@ -55,14 +53,11 @@ void amendment_list_draw_cell(ListHandle theList, Cell theCell, short dataLen, Rect *cellRect, Boolean selected) { - RgnHandle savedClip; - PenState savedPenState; - GrafPtr savedPort; Rect textRect; char tmp[50]; struct repo_amendment *amendment = NULL; struct tm *ttm = NULL; - short offset, len, height; + short len, height; LGetCell(&amendment, &dataLen, theCell, theList); if (amendment == NULL) --- diffreg.c Wed Aug 31 16:29:00 2022 +++ diffreg.c Mon Feb 6 10:24:54 2023 @@ -1205,9 +1205,9 @@ match_function(const long *f, int pos, FILE *fp) if (!state) state = " (public)"; } else { - strncpy(lastbuf, (const char *)buf, sizeof lastbuf); + strlcpy(lastbuf, (const char *)buf, sizeof lastbuf); if (state) - strncat(lastbuf, (const char *)state, sizeof lastbuf); + strlcat(lastbuf, (const char *)state, sizeof lastbuf); lastmatchline = pos; return lastbuf; } --- editor.c Thu Nov 10 09:32:40 2022 +++ editor.c Mon Feb 6 10:26:20 2023 @@ -69,7 +69,7 @@ editor_init(struct browser *browser, struct repo_amend bounds.top += off; bounds.bottom -= off; - memcpy(filename, browser->repo->bile->filename, sizeof(filename)); + memcpy(&filename, browser->repo->bile->filename, sizeof(filename)); PtoCstr(filename); snprintf((char *)&title, sizeof(title), "%s: %s: Edit Amendment %d", PROGRAM_NAME, (browser->repo ? (char *)filename : "No repo open"), @@ -116,7 +116,6 @@ editor_init(struct browser *browser, struct repo_amend /* log message */ bounds.top = bounds.bottom + PADDING; - fh = FontHeight(monaco, 9); bounds.bottom = editor->win->portRect.bottom - 20 - (PADDING * 2); bounds.right = editor->win->portRect.right - SCROLLBAR_WIDTH - PADDING; @@ -191,9 +190,8 @@ editor_idle(struct focusable *focusable, EventRecord * void editor_update(struct focusable *focusable, EventRecord *event) { - Str255 buf; Rect r; - short what = -1, len; + short what = -1; struct editor *editor = (struct editor *)focusable->cookie; if (event != NULL) @@ -282,7 +280,7 @@ editor_mouse_down(struct focusable *focusable, EventRe Point p; ControlHandle control; Rect r; - short val, adj, page, was_selected, part, i; + short val, adj; p = event->where; GlobalToLocal(&p); @@ -327,7 +325,7 @@ editor_mouse_down(struct focusable *focusable, EventRe return; } - switch (part = FindControl(p, editor->win, &control)) { + switch (FindControl(p, editor->win, &control)) { case inButton: if (TrackControl(control, p, 0L) && control == editor->save_button) @@ -423,7 +421,7 @@ editor_save(struct editor *editor) size_t len, size; time_t ts; short ret, yy, mm, dd, hh, min, ss, count = 0; - char *date, *author, *log, *data; + char *date, *data; if ((*(editor->author_te))->teLength == 0) { warn("Author field cannot be blank"); --- patch.c Wed Aug 31 16:29:22 2022 +++ patch.c Mon Feb 6 10:26:35 2023 @@ -92,7 +92,7 @@ patch_process(struct repo *repo, Str255 filename, shor char buf[1024]; size_t i; long patch_size; - short linenum = 0, error, ret; + short linenum = 0, error, ret = -1; short linelen, patch_frefnum = -1, source_frefnum = -1, dest_frefnum = -1; char *line; --- repo.c Thu Nov 10 10:33:06 2022 +++ repo.c Mon Feb 6 10:30:32 2023 @@ -74,7 +74,6 @@ repo_create(void) { SFReply reply; struct bile *bile; - short error, fh, i; SFPutFile(centered_sfput_dialog(), "\pCreate new repository:", "\p", NULL, &reply); @@ -89,7 +88,8 @@ repo_create(void) REPO_TYPE); } if (bile == NULL) - panic("Failed to create %s: %d", PtoCstr(reply.fName), error); + panic("Failed to create %s: %d", PtoCstr(reply.fName), + bile_error(NULL)); return repo_init(bile, true); } @@ -97,13 +97,10 @@ repo_create(void) struct repo * repo_init(struct bile *bile, bool is_new) { - Str255 buf; struct bile_object *bob; struct repo *repo; - struct repo_file *file; size_t size; char *data; - short error, fh; unsigned short i; repo = xmalloczero(sizeof(struct repo), "repo"); @@ -176,7 +173,6 @@ repo_close(struct repo *repo) { struct repo_file *file; struct repo_amendment *amendment; - Handle h; short i; for (i = 0; i < repo->namendments; i++) { @@ -261,7 +257,6 @@ repo_parse_amendment(unsigned long id, unsigned char * { struct repo_amendment *amendment; unsigned short len, i; - short datapos; amendment = xmalloczero(sizeof(struct repo_amendment), "repo_parse_amendment"); @@ -377,7 +372,7 @@ repo_show_diff_text(struct repo *repo, struct repo_ame char *dtext; char *buf = NULL; unsigned long diff_len, all_len; - short header_len, i, blen, height, trunc = 0; + short header_len, blen, height, trunc = 0; unsigned short warn_off; TESetText("", 0, te); @@ -472,20 +467,15 @@ repo_add_file_filter(struct FileParam *pbp) struct repo_file * repo_add_file(struct repo *repo) { - CInfoPBRec pb; WDPBRec wdir = { 0 }; SFReply reply; Str255 fname, repofname, repopath, newpath; - Handle new_fileh; struct repo_file *file; - struct repo_file_attrs attrs; - unsigned long zero; - char *data; - short i, error; + short i; /* tell SFGetFile we only want to accept files from this dir */ wdir.ioVRefNum = wdir.ioWDVRefNum = repo->bile->vrefnum; - memcpy(fname, repo->bile->filename, sizeof(fname)); + memcpy(&fname, repo->bile->filename, sizeof(fname)); wdir.ioNamePtr = (StringPtr)&fname; if (PBGetWDInfo(&wdir, 0) != noErr) { warn("Failed looking up repo directory"); @@ -499,7 +489,7 @@ repo_add_file(struct repo *repo) if (!reply.good) return NULL; - memcpy(repofname, repo->bile->filename, sizeof(repofname)); + memcpy(&repofname, repo->bile->filename, sizeof(repofname)); /* if the file is not in the same dir as the repo, bail */ getpath(repo->bile->vrefnum, repofname, &repopath, false); @@ -554,7 +544,7 @@ repo_file_update(struct repo *repo, struct repo_file * struct repo_file_attrs attrs; Str255 filename; size_t size, datapos; - short error, len, new = 0; + short error, len; unsigned char *data; strlcpy((char *)filename, file->filename, sizeof(filename)); @@ -608,6 +598,8 @@ repo_file_update(struct repo *repo, struct repo_file * bile_error(repo->bile)); xfree(&data); + + return 0; } short @@ -645,7 +637,7 @@ repo_checkout_file(struct repo *repo, struct repo_file short vrefnum, Str255 filename) { struct bile_object *textob; - size_t size, chunk; + size_t size; short error, frefnum; char *text; @@ -728,17 +720,16 @@ repo_sort_amendments(struct repo *repo) short repo_diff_file(struct repo *repo, struct repo_file *file) { - Str31 fromfilename, tofilename; + Str255 fromfilename, tofilename; Str255 fromfilepath, tofilepath; Str255 label0, label1; struct repo_file_attrs attrs; size_t size; char *text; - long len; short error, ret, frefnum, tofile_empty = 0; /* write out old file */ - snprintf((char *)fromfilename, sizeof(fromfilename), "[tmp] %s", + snprintf((char *)&fromfilename, sizeof(fromfilename), "[tmp] %s", file->filename); CtoPstr(fromfilename); @@ -780,7 +771,7 @@ repo_diff_file(struct repo *repo, struct repo_file *fi panic("getpath(%d, %s) failed", repo->bile->vrefnum, PtoCstr(fromfilename)); - memcpy((char *)tofilename, file->filename, sizeof(tofilename)); + memcpy((char *)&tofilename, file->filename, sizeof(tofilename)); CtoPstr(tofilename); if (getpath(repo->bile->vrefnum, tofilename, &tofilepath, true) != 0) panic("getpath(%d, %s) failed", repo->bile->vrefnum, @@ -810,10 +801,10 @@ repo_diff_file(struct repo *repo, struct repo_file *fi /* specify diff header labels to avoid printing tmp filename */ /* (TODO: use paths relative to repo) */ label[0] = (char *)&label0; - snprintf((char *)label0, sizeof(label0), "%s\t%s", file->filename, + snprintf((char *)&label0, sizeof(label0), "%s\t%s", file->filename, ctime(file->mtime ? &file->mtime : &attrs.mtime)); label[1] = (char *)&label1; - snprintf((char *)label1, sizeof(label1), "%s\t%s", file->filename, + snprintf((char *)&label1, sizeof(label1), "%s\t%s", file->filename, ctime(&attrs.mtime)); /* ctime is stupid, remove trailing \n */ @@ -888,7 +879,7 @@ repo_export_amendment(struct repo *repo, struct repo_a struct bile_object *bob; size_t size; char *buf = NULL; - short error, frefnum, header_len; + short error, frefnum; bob = bile_find(repo->bile, REPO_DIFF_RTYPE, amendment->id); if (bob == NULL) @@ -940,11 +931,11 @@ repo_amend(struct repo *repo, struct diffed_file *diff Str255 tfilename; struct repo_amendment *amendment; unsigned long datalen, fsize; - unsigned char *tdata, *fdata; + unsigned char *tdata; char *amendment_data; FInfo finfo; size_t size; - short i, id, error, frefnum; + short i, error, frefnum; amendment = xmalloczero(sizeof(struct repo_amendment), "repo_amend amendment"); @@ -1136,7 +1127,7 @@ repo_migrate(struct repo *repo, bool is_new) size_t size; unsigned long id; OSType type, tmpltype; - short add = 0, i, nfiles, ntmpls; + short i, ntmpls; char ver; if (is_new) @@ -1217,9 +1208,7 @@ void repo_backup(struct repo *repo) { Str255 repo_filename, source_filename, dest_filename, path; - FInfo fi; short error; - short source_ref, dest_ref; if (getpath(repo->bile->vrefnum, repo->bile->filename, &path, false) != 0) --- settings.c Wed Aug 31 16:29:38 2022 +++ settings.c Mon Feb 6 10:39:10 2023 @@ -30,8 +30,7 @@ settings_load(void) char *author; author = xGetStringAsChar(STR_AUTHOR_ID); - memcpy(settings.author, author, sizeof(settings.author)); - settings.author[sizeof(settings.author)] = '\0'; + strlcpy(settings.author, author, sizeof(settings.author)); xfree(&author); if (settings.author[0] == '\0') snprintf(settings.author, sizeof(settings.author), "unknown"); @@ -83,7 +82,6 @@ settings_edit(void) { Str255 txt; Handle ihandle; - size_t len; short itype, hit; bool done; DialogPtr dlg; @@ -94,12 +92,12 @@ settings_edit(void) err(1, "Can't find settings DLOG %d", SETTINGS_DLOG_ID); GetDItem(dlg, SETTINGS_AUTHOR_ID, &itype, &ihandle, &irect); - memcpy(txt, settings.author, strlen(settings.author) + 1); + strlcpy((char *)txt, settings.author, sizeof(txt)); CtoPstr(txt); SetIText(ihandle, txt); GetDItem(dlg, SETTINGS_TABWIDTH_ID, &itype, &ihandle, &irect); - snprintf((char *)&txt, sizeof(txt), "%d", settings.tabwidth); + snprintf((char *)txt, sizeof(txt), "%d", settings.tabwidth); CtoPstr(txt); SetIText(ihandle, txt); --- util.c Tue Sep 13 21:33:57 2022 +++ util.c Mon Feb 6 10:33:07 2023 @@ -255,7 +255,9 @@ void * xrealloc(void *src, size_t size) { void *ptr, *tsrc; +#ifdef MALLOC_DEBUG unsigned long n; +#endif char note[MALLOC_NOTE_SIZE] = "realloc from null"; #ifdef MALLOC_DEBUG