From 2bb3074fab7fcd7781528ecb1d700d7d7edbfb91 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Wed, 27 Oct 2010 13:26:56 -0500 Subject: [PATCH] db: Fix db_get_field() on an empty array field. This fixes a bug introduced with the dynamic-array branch. The new version of db_get_field() was using the dbr_size_n() macro to calculate the offset to the start of the buffer area to be zeroed, but when nRequest is zero this gives the wrong result; the dbr_ structures always contain at least one value element, and dbr_size_n() thus has to include that in its size calculation. --- src/db/db_access.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/db/db_access.c b/src/db/db_access.c index 11d457334..d9d0bc83f 100644 --- a/src/db/db_access.c +++ b/src/db/db_access.c @@ -152,12 +152,16 @@ int epicsShareAPI db_get_field(struct dbAddr *paddr, long nRequest = no_elements; int result = db_get_field_and_count( paddr, buffer_type, pbuffer, &nRequest, pfl); - if (nRequest < no_elements) - /* If the database request returned fewer elements than requested then - * fill out the remainder of the array with zeros. */ - memset( - (char *)pbuffer + dbr_size_n(buffer_type, nRequest), 0, - (no_elements - nRequest) * dbr_value_size[buffer_type]); + if (nRequest < no_elements) { + /* The database request returned fewer elements than requested, so + * fill the remainder of the buffer with zeros. + */ + int val_size = dbr_value_size[buffer_type]; + int offset = dbr_size[buffer_type] + (nRequest - 1) * val_size; + int nbytes = (no_elements - nRequest) * val_size; + + memset((char *)pbuffer + offset, 0, nbytes); + } return result; }