Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/nslcd-common.h
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2006-12-27 10:48:43 +0100
committerArthur de Jong <arthur@arthurdejong.org>2006-12-27 10:48:43 +0100
commite5a71411f3cab38fd8222c6a51d4791c330d5de7 (patch)
tree5922378019a29af9335b14589b5205b7661166b3 /nslcd-common.h
parent410b6fa99387e1fcfa786a571ac34f84547bfd1e (diff)
do not allocate new memory with malloc() for each request with a string parameter but use a buffer allocated on the stack instead (this simplifies free()-ing the buffer(s) in case of problems)
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@204 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'nslcd-common.h')
-rw-r--r--nslcd-common.h31
1 files changed, 16 insertions, 15 deletions
diff --git a/nslcd-common.h b/nslcd-common.h
index 25efd83..ee2e1d8 100644
--- a/nslcd-common.h
+++ b/nslcd-common.h
@@ -150,9 +150,10 @@ static void debug_dump(const void *ptr,size_t size)
#define BUF_CHECK(fp,sz) \
if ((bufptr+(size_t)(sz))>buflen) \
{ \
- DEBUG_PRINT("READ : buffer error: %d bytes missing",((sz)-(buflen))); \
+ /* will not fit */ \
+ DEBUG_PRINT("READ : buffer error: %d bytes too large",((sz)-(buflen))); \
ERROR_OUT_BUFERROR(fp); \
- } /* will not fit */
+ }
/* move the buffer pointer */
#define BUF_SKIP(sz) \
@@ -176,24 +177,24 @@ static void debug_dump(const void *ptr,size_t size)
(field)=BUF_CUR; \
BUF_SKIP(tmpint32+1);
-/* read a string from the stream dynamically allocating memory
- for the string (don't forget to call free() later on) */
-#define READ_STRING_ALLOC(fp,field) \
+/* read a string in a fixed-size "normal" buffer */
+#define READ_STRING_BUF2(fp,buffer,buflen) \
/* read the size of the string */ \
READ_TYPE(fp,tmpint32,int32_t); \
- /* allocate memory */ \
- (field)=(char *)malloc((size_t)(tmpint32+1)); \
- if ((field)==NULL) \
+ DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" strlen=%d",tmpint32); \
+ /* check if read would fit */ \
+ if (((size_t)tmpint32)>=(buflen)) \
{ \
- DEBUG_PRINT("READ_STRING: var="__STRING(field)" malloc() error: %s",strerror(errno)); \
- ERROR_OUT_ALLOCERROR(fp); \
- } /* problem allocating */ \
+ /* will not fit */ \
+ DEBUG_PRINT("READ : buffer error: %d bytes too large",(tmpint32-(buflen))+1); \
+ ERROR_OUT_BUFERROR(fp); \
+ } \
/* read string from the stream */ \
if (tmpint32>0) \
- { READ(fp,(field),(size_t)tmpint32); } \
- /* null-terminate string */ \
- (field)[tmpint32]='\0'; \
- DEBUG_PRINT("READ_STRING: var="__STRING(field)" string=\"%s\"",(field));
+ { READ(fp,buffer,(size_t)tmpint32); } \
+ /* null-terminate string in buffer */ \
+ buffer[tmpint32]='\0'; \
+ DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" string=\"%s\"",buffer);
/* read an array from a stram and store the length of the
array in num (size for the array is allocated) */