From 58477781608ed17356035924a2046ba00174bd56 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Fri, 27 Jun 2025 16:15:42 -0400 Subject: [PATCH] Code cleaning (fix memory leak within subafn) --- Makefile.dbg | 1 + lib/lvleml.c | 55 ++++++++++++++++++++++++++++++++++------------------ lib/lvleml.h | 2 +- lib/subafn.c | 5 ++--- lib/unitls.c | 50 ++++++++++++++++++++++++----------------------- 5 files changed, 66 insertions(+), 47 deletions(-) diff --git a/Makefile.dbg b/Makefile.dbg index 2a71ccd..55e3a79 100644 --- a/Makefile.dbg +++ b/Makefile.dbg @@ -82,6 +82,7 @@ onesortr: debug #a full complete test restart : killall dosortr dorcvr + @ echo "Restart completed" actions : debug newtest restart diff --git a/lib/lvleml.c b/lib/lvleml.c index 509e135..6812c2e 100644 --- a/lib/lvleml.c +++ b/lib/lvleml.c @@ -25,6 +25,24 @@ #include "geseml.h" #include "lvleml.h" +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to manage curent SMTP session data */ +/* within contact. */ +/* */ +/********************************************************/ +static void getsessid(CONTYP *contact) + +{ +if (contact!=(CONTYP *)0) { + contact->session=sql_freeses(contact->session); + contact->session=(SESTYP *)calloc(1,sizeof(SESTYP)); + contact->session->sessid=eml_getcursesid(contact->mainsesid,contact->numreset); + } +} /* ^L */ @@ -44,7 +62,7 @@ if (contact!=(CONTYP *)0) { contact->recipients=(RCPTYP **)rou_freelist((void **)contact->recipients, (genfree_t)eml_freerecipient); contact->mailfrom=rou_freestr(contact->mailfrom); - contact->cursesid=rou_freestr(contact->cursesid); + contact->session=sql_freeses(contact->session); contact->mainsesid=rou_freestr(contact->mainsesid); contact->fqdn=rou_freestr(contact->fqdn); contact->peername=rou_freestr(contact->peername); @@ -243,7 +261,7 @@ while (proceed==true) { break; case 3 : //Inserting ID information (void) fprintf(data,"\tid <%s@%s>;\n", - contact->cursesid,contact->locname); + contact->session->sessid,contact->locname); break; case 4 : //date information (void) fprintf(data,"\t%s\n",rou_ascsysstamp(curtime)); @@ -377,8 +395,8 @@ proceed=true; while (proceed==true) { switch (phase) { case 0 : //is contact good - if (contact==(CONTYP *)0) { - (void) rou_alert(0,"%s contact pointer is NULL (bug?)",OPEP); + if ((contact==(CONTYP *)0)||(contact->session==(SESTYP *)0)) { + (void) rou_alert(0,"%s contact pointer or session is NULL (bug?)",OPEP); phase=999; } break; @@ -387,16 +405,16 @@ while (proceed==true) { phase=999; //No recipient! break; case 2 : //creating the count file - if ((qfile=eml_createqfile(contact->cursesid,EXTCNT))==(FILE *)0) { + if ((qfile=eml_createqfile(contact->session->sessid,EXTCNT))==(FILE *)0) { (void) rou_alert(0,"%s Unable to open 'count' qfile <%s> (error=<%s>)", - OPEP,contact->cursesid,strerror(errno)); + OPEP,contact->session->sessid,strerror(errno)); phase=999; } break; case 3 : //inserting number of receipient if (fprintf(qfile,"%d\n",num)<1) { (void) rou_alert(0,"%s Unable to set 'count' in qfile <%s> (error=<%s>)", - OPEP,contact->cursesid,strerror(errno)); + OPEP,contact->session->sessid,strerror(errno)); (void) fclose(qfile); phase=999; //trouble trouble } @@ -404,7 +422,7 @@ while (proceed==true) { case 4 : //closing count file if (fclose(qfile)!=0) { (void) rou_alert(0,"%s Unable to close qfile <%s> (error=<%s>)", - OPEP,contact->cursesid,strerror(errno)); + OPEP,contact->session->sessid,strerror(errno)); phase=999; //trouble trouble } break; @@ -449,7 +467,7 @@ while (proceed==true) { //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); switch (phase) { case 0 : //Creating the file - if ((trans=eml_createqfile(contact->cursesid,ext))==(FILE *)0) + if ((trans=eml_createqfile(contact->session->sessid,ext))==(FILE *)0) phase=999; //trouble trouble break; case 1 : //write data to trans file; @@ -538,7 +556,7 @@ while (proceed==true) { } break; case 1 : //opening the queue email - if ((queue=eml_createqfile(contact->cursesid,""))==(FILE *)0) + if ((queue=eml_createqfile(contact->session->sessid,""))==(FILE *)0) phase=999; //trouble trouble break; case 2 : //adding received stamp to file @@ -582,7 +600,7 @@ while (proceed==true) { phase=999; //Unable to create count file??? break; case 7 : //renameing directive - if (eml_renameqfile(contact->cursesid,EXTMP,EXTRANS)==false) + if (eml_renameqfile(contact->session->sessid,EXTMP,EXTRANS)==false) phase=999; //Trouble trouble break; case 8 : //everything fine @@ -593,11 +611,11 @@ while (proceed==true) { total/=1024; //KBytes delta=rou_getdifftime(&start); fmt="%d-3.5.3 Session ID=<%s>"; - (void) transmit(contact,false,fmt,CMDOK,contact->cursesid); + (void) transmit(contact,false,fmt,CMDOK,contact->session->sessid); fmt="%d-3.5.3 data stream received: %d Kbytes within %d.%03d seconds)"; (void) transmit(contact,false,fmt,CMDOK,total,delta/1000,delta%1000); fmt="%d 3.5.3 Message accepted for delivery"; - (void) transmit(contact,true,fmt,CMDOK,contact->cursesid); + (void) transmit(contact,true,fmt,CMDOK,contact->session->sessid); done=true; proceed=false; //task done break; @@ -866,15 +884,14 @@ static _Bool doreset(CONTYP *contact,char *parameter) #define LOCSEQ "2.1.0" (void) transmit(contact,true,"%d-%s flushed session %s", - CMDOK,LOCSEQ,contact->cursesid); + CMDOK,LOCSEQ,contact->session->sessid); contact->numreset++; contact->recipients=(RCPTYP **)rou_freelist((void **)(contact->recipients), (genfree_t)eml_freerecipient); contact->mailfrom=rou_freestr(contact->mailfrom); -contact->cursesid=rou_freestr(contact->cursesid); -contact->cursesid=eml_getcursesid(contact->mainsesid,contact->numreset); +(void) getsessid(contact); (void) transmit(contact,true,"%d %s opening new session %s", - CMDOK,LOCSEQ,contact->cursesid); + CMDOK,LOCSEQ,contact->session->sessid); return true; #undef LOCSEQ @@ -1655,7 +1672,7 @@ while (proceed==true) { break; case 3 : //Preparing contact contact->mainsesid=eml_getmainsesid(); - contact->cursesid=eml_getcursesid(contact->mainsesid,contact->numreset); + (void) getsessid(contact); contact->locname=soc_getaddrinfo(contact->socptr,true,true); contact->locip=soc_getaddrinfo(contact->socptr,true,false); if (contact->locip!=(char *)0) { @@ -1777,7 +1794,7 @@ if ((contact!=(CONTYP *)0)&&(contact->recipients!=(RCPTYP **)0)) { tra->code=(*ptr)->code; tra->date=isnow; tra->delay=0; - tra->sessid=strdup(contact->cursesid); + tra->sessid=strdup(contact->session->sessid); tra->mailfrom=strdup(contact->mailfrom); tra->rcptto=strdup(data); tralist=(TRATYP **)rou_addlist((void **)tralist,(void *)tra); diff --git a/lib/lvleml.h b/lib/lvleml.h index 7a76eb3..589f753 100644 --- a/lib/lvleml.h +++ b/lib/lvleml.h @@ -27,7 +27,7 @@ typedef struct { char *peerip; //socket remote peer IP int numreset; //number of SMTP reset received char *mainsesid; //session main ID - char *cursesid; //current session ID + SESTYP *session; //SMTP current session information char *mailfrom; //current mail from originator RCPTYP **recipients; //List of email recipient LOGPTR *logptr; //reference to session log diff --git a/lib/subafn.c b/lib/subafn.c index d4afd8f..90bd415 100644 --- a/lib/subafn.c +++ b/lib/subafn.c @@ -204,8 +204,7 @@ while (proceed==true) { break; case 3 : //lets convert it afnnum->strnumip=afn_getstrip(afnnum); - //(void) printf("JMPDBG <%s> -> converted to <%s>\n", - //onecleanipstr,afnnum->strnumip); + //(void) printf("JMPDBG <%s> -> to <%s>\n", onecleanipstr,afnnum->strnumip); break; default : /*SAFE guard */ proceed=false; @@ -403,9 +402,9 @@ while (proceed==true) { next++; } if ((afnloc=getoneafn(ptr))!=(AFNTYP *)0) { + afnlist=(AFNTYP **)realloc(afnlist,(num+2)*sizeof(AFNTYP *)); afnlist[num]=afnloc; num++; - afnlist=(AFNTYP **)realloc(afnlist,num*sizeof(AFNTYP *)); afnlist[num]=(AFNTYP *)0; } ptr=next; diff --git a/lib/unitls.c b/lib/unitls.c index 782f5e4..bc5056d 100644 --- a/lib/unitls.c +++ b/lib/unitls.c @@ -25,7 +25,8 @@ #define SSL_CIPHER_LIST "DEFAULT" static _Bool modopen; //module open/close status -static AFNTYP **afns; //Binding information as AFN +//Binding information as AFN +static AFNTYP **afns=(AFNTYP **)0; /* ^L */ @@ -432,29 +433,29 @@ while (proceed==true) { char *newloc; char host[NI_MAXHOST]; - if (ifa->ifa_addr==(struct sockaddr *)0) - continue; - family=ifa->ifa_addr->sa_family; - switch (family) { - case AF_INET : - taille=sizeof(struct sockaddr_in); - break; - case AF_INET6 : - taille=sizeof(struct sockaddr_in6); - continue; //No scanning for IPV6 Number (Jun 2025) - break; - default : + if (ifa->ifa_addr==(struct sockaddr *)0) + continue; + family=ifa->ifa_addr->sa_family; + switch (family) { + case AF_INET : + taille=sizeof(struct sockaddr_in); + break; + case AF_INET6 : + taille=sizeof(struct sockaddr_in6); + continue; //No scanning for IPV6 Number (Jun 2025) + break; + default : + continue; + break; + } + er=getnameinfo(ifa->ifa_addr,taille,host,NI_MAXHOST,NULL,0,NI_NUMERICHOST); + if (er!=0) { + (void) rou_alert(0,"%s etnameinfo() failed: (error=<%s> system?)", + OPEP,strerror(errno)); continue; - break; - } - er=getnameinfo(ifa->ifa_addr,taille,host,NI_MAXHOST,NULL,0,NI_NUMERICHOST); - if (er!=0) { - (void) rou_alert(0,"%s etnameinfo() failed: (error=<%s> system?)", - OPEP,strerror(errno)); - continue; - } - newloc=(char *)calloc(strlen(localip)+strlen(host)+3,sizeof(char)); - if (strlen(localip)>0) { + } + newloc=(char *)calloc(strlen(localip)+strlen(host)+3,sizeof(char)); + if (strlen(localip)>0) { (void) strcpy(newloc,localip); (void) strcat(newloc,","); } @@ -465,6 +466,7 @@ while (proceed==true) { break; case 2 : //converting interface IP list to AFNTYP list afns=afn_getipnum(localip); + (void) freeifaddrs(ifaddr); break; default : //SAFE Guard proceed=false; @@ -1267,7 +1269,7 @@ return afns; int tls_modeunitls(_Bool mode) { -#define OPEP "unidoc.c:soc_modeunisoc" +#define OPEP "unitls.c:soc_modeunitls," int status; -- 2.47.3