From 171abb6143f83abba5bbd9015c3fb43a3b737a97 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Sat, 3 Aug 2024 19:55:00 -0400 Subject: [PATCH] tools about memory leak --- kleenex/Makefile | 6 +++++- kleenex/memleak.c | 16 ++++++++++++++++ lib/devsoc.c | 4 +++- lib/gestcp.c | 17 ++++++++--------- lib/modrec.c | 11 +++++++---- lib/subrou.c | 2 +- 6 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 kleenex/memleak.c diff --git a/kleenex/Makefile b/kleenex/Makefile index 4568285..82dc6b1 100644 --- a/kleenex/Makefile +++ b/kleenex/Makefile @@ -2,7 +2,11 @@ debug : server server : server.c +memleak : memleak.c + clean : - @ rm -f server server.o + @ rm -f server memleak + @ rm -f *.o CFLAGS = -Wall -D_GNU_SOURCE -g +LDFLAGS = -g diff --git a/kleenex/memleak.c b/kleenex/memleak.c new file mode 100644 index 0000000..1ec908f --- /dev/null +++ b/kleenex/memleak.c @@ -0,0 +1,16 @@ +#include +#include +#include +#include + +int main() { + + for (int i = 0; i < 10; i++) { + + int *leak = (int *) malloc(sizeof(int) * 10); + printf("Memory allocated at iteration %d ptr=%p\n",i,leak); + (void) sleep(2); + } + + return 0; +} diff --git a/lib/devsoc.c b/lib/devsoc.c index 0113200..9869696 100644 --- a/lib/devsoc.c +++ b/lib/devsoc.c @@ -30,6 +30,7 @@ typedef struct { PROTYP proto; //Connexion protocol type char *ip; //Binding IP //IPV4 or IPV6 char *port; //Binding Port + char *hostname; //binding hostname time_t lasttry; //successful binding last time int iteration; //number of soc slot used on the IP int handle; //connexion handle @@ -53,8 +54,9 @@ if (socptr!=(SOCPTR *)0) { register SOCTYP *soc; soc=(SOCTYP *)socptr; - soc->port=rou_freestr(soc->port); + soc->hostname=rou_freestr(soc->hostname); soc->ip=rou_freestr(soc->ip); + soc->port=rou_freestr(soc->port); (void) free(soc); socptr=(SOCPTR *)0; } diff --git a/lib/gestcp.c b/lib/gestcp.c index 59e6203..7df10ce 100644 --- a/lib/gestcp.c +++ b/lib/gestcp.c @@ -92,14 +92,10 @@ if (contact!=(CONTYP *)0) { OPEP,strerror(errno)); } } - if (contact->carpile!=(char *)0) - (void) free(contact->carpile); - if (contact->EOL!=(char *)0) - (void) free(contact->EOL); - if (contact->peerip!=(char *)0) - (void) free(contact->peerip); - if (contact->locname!=(char *)0) - (void) free(contact->locname); + contact->carpile=rou_freestr(contact->carpile); + contact->EOL=rou_freestr(contact->EOL); + contact->peerip=rou_freestr(contact->peerip); + contact->locname=rou_freestr(contact->locname); (void) free(contact); contact=(CONTYP *)0; } @@ -201,7 +197,7 @@ while (proceed==true) { phase=999; //No End Of Line yet break; case 2 : //duplicating carpile - *lineptr=calloc(tact->carin,sizeof(char)); + *lineptr=calloc(tact->carin+1,sizeof(char)); *eol='\000'; (void) strcpy(*lineptr,tact->carpile); (void) strcat(*lineptr,tact->EOL); @@ -371,6 +367,9 @@ while (proceed==true) { char signon[100]; + (void) prc_settitle("Contact from peer '%s' started at %s", + contact->peerip, + rou_ascsysstamp(time((time_t *)0))); (void) snprintf(signon,sizeof(signon),FMT, SIGNON,contact->locname, appname,rou_getversion(), diff --git a/lib/modrec.c b/lib/modrec.c index 162c088..e851156 100644 --- a/lib/modrec.c +++ b/lib/modrec.c @@ -5,6 +5,7 @@ /* */ /********************************************************/ #include +#include #include #include #include @@ -58,8 +59,9 @@ while (proceed==true) { TIMESPEC attend; char *line; int got; + _Bool quit; - attend.tv_sec=20; + attend.tv_sec=60; attend.tv_nsec=0; (void) printf("attend %d seconds\n",(int)attend.tv_sec); got=tcp_getline(contact,&attend,&line); @@ -68,12 +70,13 @@ while (proceed==true) { break; //remote disconnect } (void) printf("Read '%d' char =<%s>\n",got,line); - if (strcasecmp(line,"QUIT"CRLF)==0) { + quit=(strcasecmp(line,"QUIT"CRLF)==0); + (void) tcp_write(contact,line,strlen(line)); + (void) free(line); + if (quit==true) { (void) printf("remote quit\n"); break; //remote disconnect } - (void) tcp_write(contact,line,strlen(line)); - (void) free(line); } break; case 3 : //connection terminated diff --git a/lib/subrou.c b/lib/subrou.c index ceb2751..32532c1 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -20,7 +20,7 @@ //version definition #define VERSION "0.3" -#define RELEASE "18" +#define RELEASE "19" //Public variables PUBLIC int debug=0; //debug level -- 2.47.3