diff options
author | Rainer Gerhards <rgerhards@adiscon.com> | 2008-03-27 14:58:53 +0000 |
---|---|---|
committer | Rainer Gerhards <rgerhards@adiscon.com> | 2008-03-27 14:58:53 +0000 |
commit | 94e32990ee01bc7622909f40f0839bc4516cd56d (patch) | |
tree | 1e8fb76d82abc479dcffea3e2f6a894245a012ab | |
parent | 0f3da7e4b38470b35b5a1b28992c78d57c2fcaa5 (diff) | |
download | rsyslog-94e32990ee01bc7622909f40f0839bc4516cd56d.tar.gz rsyslog-94e32990ee01bc7622909f40f0839bc4516cd56d.tar.xz rsyslog-94e32990ee01bc7622909f40f0839bc4516cd56d.zip |
fixed memory leaks in stream class and imfile
-rw-r--r-- | plugins/imfile/imfile.c | 16 | ||||
-rw-r--r-- | stream.c | 28 |
2 files changed, 35 insertions, 9 deletions
diff --git a/plugins/imfile/imfile.c b/plugins/imfile/imfile.c index a6e019d6..162cab9f 100644 --- a/plugins/imfile/imfile.c +++ b/plugins/imfile/imfile.c @@ -168,6 +168,17 @@ finalize_it: } +/* The following is a cancel cleanup handler for strmReadLine(). It is necessary in case + * strmReadLine() is cancelled while processing the stream. -- rgerhards, 2008-03-27 + */ +static void pollFileCancelCleanup(void *pArg) +{ + BEGINfunc; + cstr_t **ppCStr = (cstr_t**) pArg; + if(*ppCStr != NULL) + rsCStrDestruct(ppCStr); + ENDfunc; +} /* poll a file, need to check file rollover etc. open file if not open */ static rsRetVal pollFile(fileInfo_t *pThis, int *pbHadFileData) { @@ -180,6 +191,7 @@ static rsRetVal pollFile(fileInfo_t *pThis, int *pbHadFileData) CHKiRet(openFile(pThis)); /* open file */ } + pthread_cleanup_push(pollFileCancelCleanup, &pCStr); /* loop below will be exited when strmReadLine() returns EOF */ while(1) { CHKiRet(strmReadLine(pThis->pStrm, &pCStr)); @@ -187,6 +199,7 @@ static rsRetVal pollFile(fileInfo_t *pThis, int *pbHadFileData) CHKiRet(enqLine(pThis, pCStr)); /* process line */ rsCStrDestruct(&pCStr); /* discard string (must be done by us!) */ } + pthread_cleanup_pop(0); finalize_it: if(pCStr != NULL) { @@ -333,6 +346,9 @@ persistStrmState(fileInfo_t *pInfo) CHKiRet(strmDestruct(&psSF)); finalize_it: + if(psSF != NULL) + strmDestruct(&psSF); + RETiRet; } @@ -345,33 +345,34 @@ rsRetVal strmUnreadChar(strm_t *pThis, uchar c) /* read a line from a strm file. A line is terminated by LF. The LF is read, but it * is not returned in the buffer (it is discared). The caller is responsible for - * destruction of the returned CStr object! - * rgerhards, 2008-01-07 + * destruction of the returned CStr object! -- rgerhards, 2008-01-07 + * rgerhards, 2008-03-27: I now use the ppCStr directly, without any interim + * string pointer. The reason is that this function my be called by inputs, which + * are pthread_killed() upon termination. So if we use their native pointer, they + * can cleanup (but only then). */ rsRetVal strmReadLine(strm_t *pThis, cstr_t **ppCStr) { DEFiRet; uchar c; - cstr_t *pCStr = NULL; ASSERT(pThis != NULL); ASSERT(ppCStr != NULL); - CHKiRet(rsCStrConstruct(&pCStr)); + CHKiRet(rsCStrConstruct(ppCStr)); /* now read the line */ CHKiRet(strmReadChar(pThis, &c)); while(c != '\n') { - CHKiRet(rsCStrAppendChar(pCStr, c)); + CHKiRet(rsCStrAppendChar(*ppCStr, c)); CHKiRet(strmReadChar(pThis, &c)); } - CHKiRet(rsCStrFinish(pCStr)); - *ppCStr = pCStr; + CHKiRet(rsCStrFinish(*ppCStr)); finalize_it: - if(iRet != RS_RET_OK && pCStr != NULL) - rsCStrDestruct(&pCStr); + if(iRet != RS_RET_OK && *ppCStr != NULL) + rsCStrDestruct(ppCStr); RETiRet; } @@ -421,6 +422,12 @@ CODESTARTobjDestruct(strm) if(pThis->pszDir != NULL) free(pThis->pszDir); + if(pThis->pIOBuf != NULL) + free(pThis->pIOBuf); + if(pThis->pszCurrFName != NULL) + free(pThis->pszCurrFName); + if(pThis->pszFName != NULL) + free(pThis->pszFName); ENDobjDestruct(strm) @@ -675,6 +682,9 @@ strmSetFName(strm_t *pThis, uchar *pszName, size_t iLenName) if(iLenName < 1) ABORT_FINALIZE(RS_RET_FILE_PREFIX_MISSING); + if(pThis->pszFName != NULL) + free(pThis->pszFName); + if((pThis->pszFName = malloc(sizeof(uchar) * iLenName + 1)) == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |