|
From: <kin...@us...> - 2025-09-23 10:26:08
|
Revision: 7471
http://sourceforge.net/p/teem/code/7471
Author: kindlmann
Date: 2025-09-23 10:26:02 +0000 (Tue, 23 Sep 2025)
Log Message:
-----------
fewer memory errors
Modified Paths:
--------------
teem/trunk/src/hest/argvHest.c
teem/trunk/src/hest/methodsHest.c
teem/trunk/src/hest/parsest.c
Modified: teem/trunk/src/hest/argvHest.c
===================================================================
--- teem/trunk/src/hest/argvHest.c 2025-09-23 08:36:57 UTC (rev 7470)
+++ teem/trunk/src/hest/argvHest.c 2025-09-23 10:26:02 UTC (rev 7471)
@@ -67,12 +67,7 @@
static void
hargDone(void *_harg) {
hestArg *harg = (hestArg *)_harg;
- if (harg->str) {
- /* If caller wants to keep harg->str around,
- they need to have copied it (the pointer) and set harg->str to NULL */
- free(harg->str);
- }
- airArrayNix(harg->strArr); /* leave the underlying str alone */
+ airArrayNuke(harg->strArr);
return;
}
@@ -157,8 +152,17 @@
// ret = AIR_CALLOC(1, hestArg); // (we don't have a constructor?)
// memcpy(ret, havec->harg + popIdx);
for (uint ai = popIdx; ai < havec->len - 1; ai++) {
- // shuffle down the hestArg elements (not pointers to them) of havec->harg
- memcpy(havec->harg + ai, havec->harg + ai + 1, sizeof(hestArg));
+ // shuffle down info inside the hestArg elements of havec->harg
+ hestArgSetString(havec->harg + ai, (havec->harg + ai + 1)->str);
+ (havec->harg + ai)->source = (havec->harg + ai + 1)->source;
+ /* why cannot just memcpy:
+ because then the last hestArg element of havec->harg
+ (the one that is being forgotten)
+ and the second-to-last element (the last one being kept)
+ will share ->str pointers.
+ When hargDone is called on the last hestArg's address
+ as the callack from airArrayLenIncr(), then it will also
+ free the str inside the second-to-last element; oops */
}
// decrement the nominal length of havec->harg
airArrayLenIncr(havec->hargArr, -1);
@@ -174,11 +178,14 @@
void
hestArgVecPrint(const char *caller, const char *info, const hestArgVec *havec) {
+ // fprintf(stderr, "!%s: %s hestArgVec %p has %u args:\n", caller, info, havec, havec->len);
printf("%s: %s hestArgVec %p has %u args:\n", caller, info, havec, havec->len);
for (uint idx = 0; idx < havec->hargArr->len; idx++) {
const hestArg *harg;
harg = havec->harg + idx;
- printf(" %u:<%s>", idx, harg->str);
+ // fprintf(stderr, "!%s harg@%p=%u:<%s>\n", "", AIR_VOIDP(harg), idx,
+ // harg->str ? harg->str : "NULL");
+ printf(" %u:<%s>", idx, harg->str ? harg->str : "NULL");
}
printf("\n");
}
Modified: teem/trunk/src/hest/methodsHest.c
===================================================================
--- teem/trunk/src/hest/methodsHest.c 2025-09-23 08:36:57 UTC (rev 7470)
+++ teem/trunk/src/hest/methodsHest.c 2025-09-23 10:26:02 UTC (rev 7471)
@@ -647,6 +647,7 @@
return 1;
}
*/
+ free(tbuff);
}
// ------ end of if (opt[opi].flag)
if (1 == opt[opi].kind) {
Modified: teem/trunk/src/hest/parsest.c
===================================================================
--- teem/trunk/src/hest/parsest.c 2025-09-23 08:36:57 UTC (rev 7470)
+++ teem/trunk/src/hest/parsest.c 2025-09-23 10:26:02 UTC (rev 7471)
@@ -650,11 +650,14 @@
/* else try the long version */
sprintf(buff, "--%s", sep + 1);
if (!strcmp(flarg, buff)) return (free(buff), free(ofboth), optIdx);
+ free(buff);
+ free(ofboth);
} else {
/* flag only comes in short version */
char *buff = AIR_CALLOC(strlen("-") + strlen(optFlag) + 1, char);
sprintf(buff, "-%s", optFlag);
if (!strcmp(flarg, buff)) return (free(buff), optIdx);
+ free(buff);
}
}
if (hparm->verbosity) printf("%s: no match, returning UINT_MAX\n", __func__);
@@ -790,11 +793,22 @@
// remember from whence this option came
theOpt->source = theArg->source;
// lose the flag argument
+ if (hparm->verbosity > 1) {
+ hestArgVecPrint(__func__, "main havec as it came", havec);
+ }
hestArgVecRemove(havec, argIdx);
- // empty any pior parm args learned for this option
+ if (hparm->verbosity > 1) {
+ char info[AIR_STRLEN_HUGE + 1];
+ sprintf(info, "main havec after losing argIdx %u", argIdx);
+ hestArgVecPrint(__func__, info, havec);
+ }
+ // empty any prior parm args learned for this option
hestArgVecReset(theOpt->havec);
for (uint pidx = 0; pidx < parmNum; pidx++) {
// theArg still points to the next arg (at index argIdx) for this option
+ if (hparm->verbosity) {
+ printf("%s: moving |%s| to theOpt->havec\n", __func__, theArg->str);
+ }
hestArgVecAppendString(theOpt->havec, theArg->str);
hestArgVecRemove(havec, argIdx);
}
@@ -819,10 +833,15 @@
theOpt = opt + opi;
if (1 != theOpt->kind // this kind of option can take a parm
&& theOpt->flag // and this is a flagged option we should have handled above
- && theOpt->dflt // and this option has no default
- && (hestSourceUnknown == theOpt->source)) { // but this option hasn't been set
+ && !(theOpt->dflt)) { // and this option has no default
+ if (hparm->verbosity > 1) {
+ printf("%s: opt %u |%s| (flagged opt w/ parm but no default) source = %s\n",
+ __func__, opi, theOpt->flag, airEnumStr(hestSource, theOpt->source));
+ }
+ if (hestSourceUnknown == theOpt->source) { // but this option hasn't been set
biffAddf(HEST, "%s: didn't get required %s\n", __func__, identStr(ident1, theOpt));
return 1;
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|