From 5080c2c33f1f7d659b1520a1dee5a7df171d443a Mon Sep 17 00:00:00 2001 From: stefan schippers Date: Thu, 28 Sep 2023 00:30:00 +0200 Subject: [PATCH] fix unitialized vars (mostly false errors, but why not...) --- src/callback.c | 50 ++++++++++++++++++++++++++----------------------- src/check.c | 20 ++++++++++---------- src/draw.c | 18 +++++++++--------- src/editprop.c | 2 +- src/scheduler.c | 2 +- src/token.c | 2 +- src/xinit.c | 6 +++--- 7 files changed, 52 insertions(+), 48 deletions(-) diff --git a/src/callback.c b/src/callback.c index a9c7ee8c..ce06be22 100644 --- a/src/callback.c +++ b/src/callback.c @@ -189,7 +189,7 @@ static void backannotate_at_cursor_b_pos(xRect *r, Graph_ctx *gr) { if(sch_waves_loaded() >= 0) { - int dset, first, last, dataset = gr->dataset, i, p, ofs = 0; + int dset, first = -1, last, dataset = gr->dataset, i, p, ofs = 0; double start, end; int sweepvar_wrap = 0, sweep_idx; double xx, cursor2; /* xx is the p-th sweep variable value, cursor2 is cursor 'b' x position */ @@ -248,26 +248,30 @@ static void backannotate_at_cursor_b_pos(xRect *r, Graph_ctx *gr) ofs += xctx->graph_npoints[dset]; sweepvar_wrap++; } /* for(dset...) */ - if(p > last) { - double sweep0, sweep1; - p = last; - sweep0 = xctx->graph_values[sweep_idx][first]; - sweep1 = xctx->graph_values[sweep_idx][p]; - if(fabs(sweep0 - cursor2) < fabs(sweep1 - cursor2)) { - p = first; + + + if(first != -1) { + if(p > last) { + double sweep0, sweep1; + p = last; + sweep0 = xctx->graph_values[sweep_idx][first]; + sweep1 = xctx->graph_values[sweep_idx][p]; + if(fabs(sweep0 - cursor2) < fabs(sweep1 - cursor2)) { + p = first; + } } + dbg(1, "xx=%g, p=%d\n", xx, p); + tcleval("array unset ngspice::ngspice_data"); + xctx->graph_annotate_p = p; + for(i = 0; i < xctx->graph_nvars; ++i) { + char s[100]; + my_snprintf(s, S(s), "%.4g", xctx->graph_values[i][p]); + dbg(1, "%s = %g\n", xctx->graph_names[i], xctx->graph_values[i][p]); + tclvareval("array set ngspice::ngspice_data [list {", xctx->graph_names[i], "} ", s, "]", NULL); + } + tclvareval("set ngspice::ngspice_data(n\\ vars) ", my_itoa( xctx->graph_nvars), NULL); + tclvareval("set ngspice::ngspice_data(n\\ points) 1", NULL); } - dbg(1, "xx=%g, p=%d\n", xx, p); - tcleval("array unset ngspice::ngspice_data"); - xctx->graph_annotate_p = p; - for(i = 0; i < xctx->graph_nvars; ++i) { - char s[100]; - my_snprintf(s, S(s), "%.4g", xctx->graph_values[i][p]); - dbg(1, "%s = %g\n", xctx->graph_names[i], xctx->graph_values[i][p]); - tclvareval("array set ngspice::ngspice_data [list {", xctx->graph_names[i], "} ", s, "]", NULL); - } - tclvareval("set ngspice::ngspice_data(n\\ vars) ", my_itoa( xctx->graph_nvars), NULL); - tclvareval("set ngspice::ngspice_data(n\\ points) 1", NULL); } } @@ -324,7 +328,7 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int { Graph_ctx *gr; int i, redraw_all_at_end = 0, need_all_redraw = 0, need_redraw = 0, dataset = 0; - double xx1, xx2, yy1, yy2; + double xx1 = 0.0, xx2 = 0.0, yy1, yy2; double delta_threshold = 0.25; double zoom_m = 0.5; int save_mouse_at_end = 0, clear_graphpan_at_end = 0; @@ -559,12 +563,12 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int if(gr->unitx != 1.0) my_snprintf(sx, S(sx), "%.5g%c", gr->unitx * xval, gr->unitx_suffix); else - strncpy(sx, dtoa_eng(xval), S(sx)); + my_strncpy(sx, dtoa_eng(xval), S(sx)); if(gr->unitx != 1.0) my_snprintf(sy, S(sy), "%.4g%c", gr->unity * yval, gr->unity_suffix); else - strncpy(sy, dtoa_eng(yval), S(sy)); + my_strncpy(sy, dtoa_eng(yval), S(sy)); tclvareval("set measure_text \"y=", sy, "\nx=", sx, "\"", NULL); tcleval("graph_show_measure"); @@ -945,7 +949,7 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int if(xx2 < xx1) { tmp = xx1; xx1 = xx2; xx2 = tmp; } if(xx1 == xx2) xx2 += 1e-6; */ - + if(xx1 == xx2) xx2 += 1e-6; my_strdup(_ALLOC_ID_, &r->prop_ptr, subst_token(r->prop_ptr, "x1", dtoa(xx1))); my_strdup(_ALLOC_ID_, &r->prop_ptr, subst_token(r->prop_ptr, "x2", dtoa(xx2))); need_redraw = 1; diff --git a/src/check.c b/src/check.c index bca6e26e..9329a263 100644 --- a/src/check.c +++ b/src/check.c @@ -415,6 +415,7 @@ static int touches_inst_pin(double x, double y, int inst) /* return 2 if x0, y0 is on the segment * return 1 if x0, y0 is less than cadsnap (10) from the segment + * return 0 if nothing will be cut (mouse too far away or degenerated segment) * In this case x0, y0 are reset to the closest point on the segment */ static int closest_point_calculation(double x1, double y1, double x2, double y2, double *x0, double *y0, int align) @@ -426,22 +427,20 @@ static int closest_point_calculation(double x1, double y1, double x2, double y2, sq_cs = cs * cs; /* get squared value to compare with squared distance */ if(x1 == x2 && y1 == y2) { - x3 = x1; - y3 = y1; + ret = 0; } else { projection = (x2 - x1) * (*x0 - x1) + (y2 - y1) * (*y0 - y1); projection /= (x2 - x1) * ( x2 - x1) + (y2 - y1) * (y2 - y1); x3 = x1 + projection * (x2 - x1); y3 = y1 + projection * (y2 - y1); - } - sq_distance = (*x0 - x3) * (*x0 - x3) + (*y0 - y3) * (*y0 - y3); - - if(projection <= 1 && projection >= 0) { /* point is within x1,y1 - x2,y2 */ - if(sq_distance == 0) ret = 2; - else if(sq_distance < sq_cs) ret = 1; + sq_distance = (*x0 - x3) * (*x0 - x3) + (*y0 - y3) * (*y0 - y3); + if(projection <= 1 && projection >= 0) { /* point is within x1,y1 - x2,y2 */ + if(sq_distance == 0) ret = 2; + else if(sq_distance < sq_cs) ret = 1; + } + dbg(1, "x3 = %g y3=%g dist=%g ret=%d\n", x3, y3, sqrt(sq_distance), ret); } - dbg(1, "x3 = %g y3=%g dist=%g ret=%d\n", x3, y3, sqrt(sq_distance), ret); if(ret == 1) { if(align) { *x0 = my_round(x3 / cs) * cs; @@ -450,6 +449,7 @@ static int closest_point_calculation(double x1, double y1, double x2, double y2, *x0 = x3; *y0 = y3; } + /* if ret == 2 leave x0 and y0 as they are since x0,y0 already touches wire */ } return ret; } @@ -470,7 +470,7 @@ void break_wires_at_point(double x0, double y0, int align) x2 = xctx->wire[i].x2; y2 = xctx->wire[i].y2; r = closest_point_calculation(x1, y1, x2, y2, &x0, &y0, align); - if( r == 1 || touch(x1, y1, x2, y2, x0,y0) ) { + if( r != 0 && (r == 1 || touch(x1, y1, x2, y2, x0,y0) )) { if( (x0 != x1 && x0 != x2) || (y0 != y1 && y0 != y2) ) { dbg(1, "break_wires_at_point(): processing wire %d: %g %g %g %g\n", diff --git a/src/draw.c b/src/draw.c index 8ae53e01..4f660c5e 100644 --- a/src/draw.c +++ b/src/draw.c @@ -2209,7 +2209,7 @@ static void draw_graph_points(int idx, int first, int last, int poly_npoints = 0; double s1; double s2; - double c, c1; + double c = 0, c1; register SPICE_DATA *gv = xctx->graph_values[idx]; dbg(1, "draw_graph_points: idx=%d, first=%d, last=%d, wcnt=%d\n", idx, first, last, wcnt); @@ -2874,7 +2874,7 @@ int calc_custom_data_yrange(int sweep_idx, const char *express, Graph_ctx *gr) int p, dset, ofs; int first, last; double xx; /* the p-th sweep variable value: xctx->graph_values[sweep_idx][p] */ - double xx0; /* first sweep value */ + double xx0 = 0; /* first sweep value */ double start; double end; int sweepvar_wrap = 0; /* incremented on new dataset or sweep variable wrap */ @@ -2982,7 +2982,7 @@ int find_closest_wave(int i, Graph_ctx *gr) int p, dset, ofs; int first, last; double xx, yy ; /* the p-th point */ - double xx0; /* first sweep value */ + double xx0 = 0.0; /* first sweep value */ double start; double end; int sweepvar_wrap = 0; /* incremented on new dataset or sweep variable wrap */ @@ -2991,7 +2991,7 @@ int find_closest_wave(int i, Graph_ctx *gr) end = (gr->gx1 <= gr->gx2) ? gr->gx2 : gr->gx1; /* loop through all datasets found in raw file */ for(dset = 0 ; dset < xctx->graph_datasets; dset++) { - double prev_x; + double prev_x = 0.0; int cnt=0, wrap; register SPICE_DATA *gvx = xctx->graph_values[sweep_idx]; register SPICE_DATA *gvy; @@ -3022,7 +3022,7 @@ int find_closest_wave(int i, Graph_ctx *gr) } if(xx >= start && xx <= end) { if(first == -1) first = p; - if( XSIGN(xval - xx) != XSIGN(xval - prev_x)) { + if( p > ofs && XSIGN(xval - xx) != XSIGN(xval - prev_x)) { if(min < 0.0) { min = fabs(yval - yy); @@ -3078,8 +3078,8 @@ void draw_graph(int i, const int flags, Graph_ctx *gr, void *ct) char *bus_msb = NULL; int wcnt = 0, idx, expression; int measure_p = -1; - double measure_x; - double measure_prev_x; + double measure_x = 0.0; + double measure_prev_x = 0.0; char *express = NULL; xRect *r = &xctx->rect[GRIDLAYER][i]; @@ -3149,7 +3149,7 @@ void draw_graph(int i, const int flags, Graph_ctx *gr, void *ct) int poly_npoints; int first, last; double xx; /* the p-th sweep variable value: xctx->graph_values[sweep_idx][p] */ - double xx0; /* the first sweep value */ + double xx0 = 0.0; /* the first sweep value */ double start; double end; int n_bits = 1; @@ -3291,7 +3291,7 @@ static void draw_graph_all(int flags) { int i, sch_loaded, hide_graphs; int bbox_set = 0; - int save_bbx1, save_bby1, save_bbx2, save_bby2; + int save_bbx1 = 0, save_bby1 = 0, save_bbx2 = 0, save_bby2 = 0; dbg(1, "draw_graph_all(): flags=%d\n", flags); /* save bbox data, since draw_graph_all() is called from draw() which may be called after a bbox(SET) */ sch_loaded = (sch_waves_loaded() >= 0); diff --git a/src/editprop.c b/src/editprop.c index f866ef6c..c39ae966 100644 --- a/src/editprop.c +++ b/src/editprop.c @@ -1137,7 +1137,7 @@ static int edit_text_property(int x) #endif int sel, k, text_changed = 0, props_changed = 0, size_changed = 0, tmp; int c,l, preserve; - double hsize, vsize, dtmp; + double hsize = 0.4, vsize = 0.4, dtmp; double xx1,yy1,xx2,yy2; double pcx,pcy; /* pin center 20070317 */ char property[100];/* used for float 2 string conv (xscale and yscale) overflow safe */ diff --git a/src/scheduler.c b/src/scheduler.c index 6a449e62..38f09605 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -4225,7 +4225,7 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg */ else if(!strcmp(argv[1], "switch")) { - int r; + int r = 1; /* error: no switch was done */ if(!xctx) {Tcl_SetResult(interp, not_avail, TCL_STATIC); return TCL_ERROR;} if(argc > 2) r = new_schematic("switch", argv[2], NULL); Tcl_SetResult(interp, my_itoa(r), TCL_VOLATILE); diff --git a/src/token.c b/src/token.c index c25dea56..c503b305 100644 --- a/src/token.c +++ b/src/token.c @@ -667,7 +667,7 @@ const char *get_sym_template(char *s,char *extra) /* 2: eat backslashes */ /* 3: 1+2 :) */ - dbg(1, "get_sym_template(): s=%s, extra=%s\n", s, extra); + dbg(1, "get_sym_template(): s=%s, extra=%s\n", s ? s : "NULL", extra ? extra : "NULL"); if(s==NULL) { my_free(_ALLOC_ID_, &result); return ""; diff --git a/src/xinit.c b/src/xinit.c index 6193d260..3bfce5ee 100644 --- a/src/xinit.c +++ b/src/xinit.c @@ -1469,7 +1469,7 @@ static int switch_tab(int *window_count, const char *win_path) /* non NULL and not empty noconfirm is used to avoid warning for duplicated filenames */ static void create_new_window(int *window_count, const char *noconfirm, const char *fname) { - Window win_id; + Window win_id = 0LU; char toppath[WINDOW_PATH_SIZE]; char prev_window[WINDOW_PATH_SIZE]; int i, n; @@ -1538,7 +1538,7 @@ static void create_new_window(int *window_count, const char *noconfirm, const ch init_pixdata();/* populate xctx->fill_type array that is used in create_gc() to set fill styles */ save_xctx[n] = xctx; dbg(1, "new_schematic() draw, load schematic\n"); - xctx->window = win_id; + if(has_x) xctx->window = win_id; set_snap(0); /* set default value specified in xschemrc as 'snap' else CADSNAP */ set_grid(0); /* set default value specified in xschemrc as 'grid' else CADGRID */ if(has_x) create_gc(); @@ -2037,7 +2037,7 @@ static void resetcairo(int create, int clear, int force_or_resize) /* w and h (if > 0 ) parameters force reset pixmap to w x h, regardless of window size */ void resetwin(int create_pixmap, int clear_pixmap, int force, int w, int h) { - unsigned int width, height; + unsigned int width = 0, height = 0; int status; #ifndef __unix__ HWND hwnd = Tk_GetHWND(xctx->window);