summaryrefslogtreecommitdiffstats
path: root/stringbuf.c
diff options
context:
space:
mode:
authorRainer Gerhards <rgerhards@adiscon.com>2007-09-05 15:59:47 +0000
committerRainer Gerhards <rgerhards@adiscon.com>2007-09-05 15:59:47 +0000
commit82aeadce540240396d13d3355d4ef04e3524acbf (patch)
treef2c3372b68174739ed31ed98c52cf6fcce4f9e89 /stringbuf.c
parent0dc7ddd0ea702857c1d5512826e222212b634cb5 (diff)
downloadrsyslog-82aeadce540240396d13d3355d4ef04e3524acbf.tar.gz
rsyslog-82aeadce540240396d13d3355d4ef04e3524acbf.tar.xz
rsyslog-82aeadce540240396d13d3355d4ef04e3524acbf.zip
- changed part of the CStr interface so that better error tracking is
provided and the calling sequence is more intuitive (there were invalid calls based on a too-weired interface) - (hopefully) fixed some remaining bugs rooted in wrong use of the CStr class. These could lead to program abort.
Diffstat (limited to 'stringbuf.c')
-rwxr-xr-xstringbuf.c37
1 files changed, 31 insertions, 6 deletions
diff --git a/stringbuf.c b/stringbuf.c
index 7ed77731..ff5e9332 100755
--- a/stringbuf.c
+++ b/stringbuf.c
@@ -287,7 +287,7 @@ uchar* rsCStrGetSzStr(rsCStrObj *pThis)
if(pThis->pBuf != NULL)
if(pThis->pszBuf == NULL) {
/* we do not yet have a usable sz version - so create it... */
- if((pThis->pszBuf = malloc(pThis->iStrLen + 1 * sizeof(uchar))) == NULL) {
+ if((pThis->pszBuf = malloc((pThis->iStrLen + 1) * sizeof(uchar))) == NULL) {
/* TODO: think about what to do - so far, I have no bright
* idea... rgerhards 2005-09-07
*/
@@ -325,17 +325,42 @@ uchar* rsCStrGetSzStr(rsCStrObj *pThis)
* convert it to an szString without the need to copy. The extra memory
* footprint is not hefty, but the performance gain is potentially large.
* To get it done now, I am not doing the optimiziation right now.
- *
* rgerhards, 2005-09-07
+ *
+ * rgerhards, 2007-09-04: I have changed the interface of this function. It now
+ * returns an rsRetVal, so that we can communicate back if we have an error.
+ * Using the standard method is much better than returning NULL. Secondly, NULL
+ * was not actually an error - it was in indication if the string was empty.
+ * This was needed in some parts of the code, in others not. I have now added
+ * a second parameter to specify what the caller needs. I hope these changes
+ * will make it less likely that the function is called incorrectly, what
+ * previously happend quite often and was the cause of a number of program
+ * aborts. So the parameters are now:
+ * pointer to the object, pointer to string-pointer to receive string and
+ * bRetNULL: 0 - must not return NULL on empty string, return "" in that
+ * case, 1 - return NULL instead of an empty string.
+ * PLEASE NOTE: the caller must free the memory returned in ppSz in any case
+ * (except, of course, if it is NULL).
*/
-uchar* rsCStrConvSzStrAndDestruct(rsCStrObj *pThis)
+rsRetVal rsCStrConvSzStrAndDestruct(rsCStrObj *pThis, uchar **ppSz, int bRetNULL)
{
+ DEFiRet;
uchar* pRetBuf;
rsCHECKVALIDOBJECT(pThis, OIDrsCStr);
+ assert(ppSz != NULL);
+ assert(bRetNULL == 0 || bRetNULL == 1);
+
+ if(pThis->pBuf == NULL) {
+ if((pRetBuf = malloc(sizeof(uchar))) == NULL)
+ ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
+ *pRetBuf = '\0';
+ } else
+ pRetBuf = rsCStrGetSzStr(pThis);
+
+ *ppSz = pRetBuf;
- pRetBuf = rsCStrGetSzStr(pThis);
-
+finalize_it:
/* We got it, now free the object ourselfs. Please note
* that we can NOT use the rsCStrDestruct function as it would
* also free the sz String buffer, which we pass on to the user.
@@ -344,7 +369,7 @@ uchar* rsCStrConvSzStrAndDestruct(rsCStrObj *pThis)
free(pThis->pBuf);
RSFREEOBJ(pThis);
- return(pRetBuf);
+ return(iRet);
}