From e45e3caa84c70b6d4274f4478d318215eb505bcf Mon Sep 17 00:00:00 2001 From: Stefan Frederik Date: Wed, 8 Dec 2021 02:38:56 +0100 Subject: [PATCH] avoid allocating 0 byte objects in hash_iterator.c, some fixes in xschem subcommands ("clear", "get lastsel", "align", "wire") --- src/hash_iterator.c | 37 +++++++++++++++++++++---------------- src/scheduler.c | 17 ++++++++--------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/hash_iterator.c b/src/hash_iterator.c index ea53c097..0cee6d94 100644 --- a/src/hash_iterator.c +++ b/src/hash_iterator.c @@ -26,21 +26,24 @@ void init_inst_iterator(struct iterator_ctx *ctx, double x1, double y1, double x { ctx->instflag = NULL; dbg(3, "init_inst_iterator(): instances=%d\n", xctx->instances); - my_realloc(135, &ctx->instflag, xctx->instances*sizeof(unsigned short)); - memset(ctx->instflag, 0, xctx->instances*sizeof(unsigned short)); + + if(xctx->instances) { + my_realloc(135, &ctx->instflag, xctx->instances*sizeof(unsigned short)); + memset(ctx->instflag, 0, xctx->instances*sizeof(unsigned short)); + } /* calculate square 4 1st corner of drawing area */ - ctx->x1a = floor(x1/BOXSIZE) ; - ctx->y1a = floor(y1/BOXSIZE) ; + ctx->x1a = floor(x1 / BOXSIZE) ; + ctx->y1a = floor(y1 / BOXSIZE) ; /* calculate square 4 2nd corner of drawing area */ - ctx->x2a = floor(x2/BOXSIZE); - ctx->y2a = floor(y2/BOXSIZE); + ctx->x2a = floor(x2 / BOXSIZE); + ctx->y2a = floor(y2 / BOXSIZE); ctx->i = ctx->x1a; ctx->j = ctx->y1a; - ctx->tmpi = ctx->i % NBOXES; if(ctx->tmpi<0) ctx->tmpi+=NBOXES; - ctx->tmpj = ctx->j % NBOXES; if(ctx->tmpj<0) ctx->tmpj+=NBOXES; - ctx->counti=0; - ctx->instanceptr=xctx->inst_spatial_table[ctx->tmpi][ctx->tmpj]; - ctx->countj=0; + ctx->tmpi = ctx->i % NBOXES; if(ctx->tmpi < 0) ctx->tmpi += NBOXES; + ctx->tmpj = ctx->j % NBOXES; if(ctx->tmpj < 0) ctx->tmpj += NBOXES; + ctx->counti = 0; + ctx->instanceptr = xctx->inst_spatial_table[ctx->tmpi][ctx->tmpj]; + ctx->countj = 0; } @@ -79,8 +82,10 @@ void init_wire_iterator(struct iterator_ctx *ctx, double x1, double y1, double x { ctx->wireflag = NULL; dbg(3, "init_wire_iterator(): wires=%d\n", xctx->wires); - my_realloc(136, &ctx->wireflag, xctx->wires*sizeof(unsigned short)); - memset(ctx->wireflag, 0, xctx->wires*sizeof(unsigned short)); + if(xctx->wires) { + my_realloc(136, &ctx->wireflag, xctx->wires*sizeof(unsigned short)); + memset(ctx->wireflag, 0, xctx->wires*sizeof(unsigned short)); + } /* calculate square 4 1st corner of drawing area */ ctx->x1a = floor(x1 / BOXSIZE) ; ctx->y1a = floor(y1 / BOXSIZE) ; @@ -89,9 +94,9 @@ void init_wire_iterator(struct iterator_ctx *ctx, double x1, double y1, double x ctx->y2a = floor(y2 / BOXSIZE); ctx->i = ctx->x1a; ctx->j = ctx->y1a; - ctx->tmpi=ctx->i % NBOXES; if(ctx->tmpi < 0) ctx->tmpi += NBOXES; - ctx->tmpj=ctx->j % NBOXES; if(ctx->tmpj < 0) ctx->tmpj += NBOXES; - ctx->counti=0; + ctx->tmpi = ctx->i % NBOXES; if(ctx->tmpi < 0) ctx->tmpi += NBOXES; + ctx->tmpj = ctx->j % NBOXES; if(ctx->tmpj < 0) ctx->tmpj += NBOXES; + ctx->counti = 0; ctx->wireptr = xctx->wire_spatial_table[ctx->tmpi][ctx->tmpj]; ctx->countj = 0; } diff --git a/src/scheduler.c b/src/scheduler.c index 7e228868..dd2dcfea 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -304,7 +304,7 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg cmd_found = 1; xctx->push_undo(); round_schematic_to_grid(tclgetdoublevar("cadsnap")); - if(tclgetvar("autotrim_wires")) trim_wires(); + if(tclgetboolvar("autotrim_wires")) trim_wires(); set_modify(1); xctx->prep_hash_inst=0; xctx->prep_hash_wires=0; @@ -412,11 +412,11 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg else if(!strcmp(argv[1],"clear")) { - int cancel; + int cancel = 0; cmd_found = 1; - cancel=save(1); - if(cancel != -1){ /* -1 means user cancel save request */ + if( argc < 3 || strcmp(argv[2], "force") ) cancel=save(1); + if(cancel != -1) { /* -1 means user cancel save request */ char name[PATH_MAX]; struct stat buf; int i; @@ -784,11 +784,10 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg Tcl_SetResult(interp, s,TCL_VOLATILE); } else if(!strcmp(argv[2],"lastsel")) { + char s[30]; rebuild_selected_array(); - if( xctx->lastsel != 0 ) - Tcl_SetResult(interp, "1",TCL_STATIC); - else - Tcl_SetResult(interp, "0",TCL_STATIC); + my_snprintf(s, S(s), "%d", xctx->lastsel); + Tcl_SetResult(interp, s,TCL_VOLATILE); } else if(!strcmp(argv[2],"line_width")) { char s[40]; @@ -2644,7 +2643,7 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg save = xctx->draw_window; xctx->draw_window = 1; drawline(WIRELAYER,NOW, x1,y1,x2,y2, 0); xctx->draw_window = save; - if(tclgetvar("autotrim_wires")) trim_wires(); + if(tclgetboolvar("autotrim_wires")) trim_wires(); } else xctx->ui_state |= MENUSTARTWIRE; }