Hi Mathi, I'll explain you the endian problem on an example.
Let's say that we have a SaNameT string of size 400 (which is in hexadecimal 0x190). Type of size_t is 64 bits (8 bytes) On little endian system, 400 bytes is represented in memory as 90 01 00 00 00 00 00 00 On big endian system, 400 bytes is represented in memory as 00 00 00 00 00 00 01 90 Now, when we copy first 2 bytes of 64 bit integer to 2 byte unsigned integer on big endian system, we actually copy two zeros. And opposite, when we copy 2 byte unsigned integer to 64 bit integer, then we get a huge number. In this case, on big endian system, size_t variable will have value 01 90 00 00 00 00 00 00. On little endian systems, memcpy works fine. Regards, Zoran -----Original Message----- From: Mathivanan Naickan Palanivelu [mailto:[email protected]] Sent: den 16 juli 2014 10:42 To: Minh Chau H Cc: Anders Björnerstedt; Hans Feldt; Zoran Milinkovic; [email protected]; Anders Widell; [email protected] Subject: Re: [PATCH 2 of 7] NTF: Adapt NTF common library to support long DNs [#873] v3 Hi, Could you point out the endianness problem that is mentioned in the review request Thanks, Mathi. ----- [email protected] wrote: > osaf/libs/common/ntfsv/Makefile.am | 1 + > osaf/libs/common/ntfsv/include/ntfsv_mem.h | 7 + > osaf/libs/common/ntfsv/ntfsv_enc_dec.c | 37 ++++-- > osaf/libs/common/ntfsv/ntfsv_mem.c | 151 > +++++++++++++++++++++++++++- > 4 files changed, 175 insertions(+), 21 deletions(-) > > > (1)Fix endian issue in ntfs_sanamet_length, ntfsv_sanamet_alloc, > ntfsv_sanamet_steal > > diff --git a/osaf/libs/common/ntfsv/Makefile.am > b/osaf/libs/common/ntfsv/Makefile.am > --- a/osaf/libs/common/ntfsv/Makefile.am > +++ b/osaf/libs/common/ntfsv/Makefile.am > @@ -23,6 +23,7 @@ SUBDIRS = include > noinst_LTLIBRARIES = libntfsv_common.la > > libntfsv_common_la_CPPFLAGS = \ > + -DSA_EXTENDED_NAME_SOURCE \ > $(AM_CPPFLAGS) \ > -I$(top_srcdir)/osaf/libs/common/ntfsv/include > > diff --git a/osaf/libs/common/ntfsv/include/ntfsv_mem.h > b/osaf/libs/common/ntfsv/include/ntfsv_mem.h > --- a/osaf/libs/common/ntfsv/include/ntfsv_mem.h > +++ b/osaf/libs/common/ntfsv/include/ntfsv_mem.h > @@ -97,6 +97,13 @@ extern "C" { > void > ntfsv_filter_obj_cr_del_free(SaNtfObjectCreateDeleteNotificationFilter > T > *f); > void > ntfsv_filter_attr_ch_free(SaNtfAttributeChangeNotificationFilterT > *f); > > + SaAisErrorT ntfsv_sanamet_copy(SaNameT* pDes, SaNameT* pSrc); > + bool ntfsv_sanamet_is_valid(const SaNameT* pName); > + void ntfsv_sanamet_clone_strptr(SaNameT* pName); > + SaStringT ntfs_sanamet_strdup(SaNameT* pName); > + size_t ntfs_sanamet_length(const SaNameT* pName); > + void ntfs_sanamet_steal(SaStringT value, size_t length, SaNameT* > pName); > + void ntfs_sanamet_alloc(SaConstStringT value, size_t length, > SaNameT* pName); > #ifdef __cplusplus > } > #endif > diff --git a/osaf/libs/common/ntfsv/ntfsv_enc_dec.c > b/osaf/libs/common/ntfsv/ntfsv_enc_dec.c > --- a/osaf/libs/common/ntfsv/ntfsv_enc_dec.c > +++ b/osaf/libs/common/ntfsv/ntfsv_enc_dec.c > @@ -18,6 +18,8 @@ > #include <ncsencdec_pub.h> > #include "ntfsv_enc_dec.h" > #include "ntfsv_mem.h" > +#include "osaf_extended_name.h" > +#include "saAis.h" > > typedef union { > uint32_t uint32_val; > @@ -334,19 +336,22 @@ static uint32_t decodeSaNtfAttribute(NCS static > uint32_t encodeSaNameT(NCS_UBAID *uba, uint8_t *p8, SaNameT > *name) > { > uint32_t rv; > - > p8 = ncs_enc_reserve_space(uba, 2); > if (!p8) { > TRACE("ncs_enc_reserve_space failed"); > return NCSCC_RC_OUT_OF_MEM; > } > - if (name->length > SA_MAX_NAME_LENGTH) { > - LOG_ER("SaNameT length too long %hd", name->length); > - osafassert(0); > - } > - ncs_encode_16bit(&p8, name->length); > - ncs_enc_claim_space(uba, 2); > - rv = ncs_encode_n_octets_in_uba(uba, name->value, > (uint32_t)name->length); > + > + if (!ntfsv_sanamet_is_valid(name)) { > + LOG_ER("SaNameT is invalid"); > + osafassert(0); > + } > + > + SaConstStringT value = osaf_extended_name_borrow(name); > + size_t length = ntfs_sanamet_length(name); > + ncs_encode_16bit(&p8, length); > + ncs_enc_claim_space(uba, 2); > + rv = ncs_encode_n_octets_in_uba(uba, (uint8_t*) value, (uint32_t) > length); > return rv; > } > > @@ -355,14 +360,22 @@ static uint32_t decodeSaNameT(NCS_UBAID > uint8_t local_data[2]; > uint32_t rv; > p8 = ncs_dec_flatten_space(uba, local_data, 2); > - name->length = ncs_decode_16bit(&p8); > - if (name->length > SA_MAX_NAME_LENGTH) { > - LOG_ER("SaNameT length too long: %hd", name->length); > + size_t length = ncs_decode_16bit(&p8); > + if (length > kMaxDnLength) { > + LOG_ER("SaNameT length too long: %zu", length); > /* this should not happen */ > osafassert(0); > } > ncs_dec_skip_space(uba, 2); > - rv = ncs_decode_n_octets_from_uba(uba, name->value, > (uint32_t)name->length); > + char* value = (char*) malloc(length + 1); > + if (value == NULL) { > + LOG_ER("Out of memory"); > + /* this should not happen */ > + osafassert(0); > + } > + rv = ncs_decode_n_octets_from_uba(uba, (uint8_t*) value, (uint32_t) > length); > + value[length] = '\0'; > + ntfs_sanamet_steal(value, length, name); > return rv; > } > > diff --git a/osaf/libs/common/ntfsv/ntfsv_mem.c > b/osaf/libs/common/ntfsv/ntfsv_mem.c > --- a/osaf/libs/common/ntfsv/ntfsv_mem.c > +++ b/osaf/libs/common/ntfsv/ntfsv_mem.c > @@ -18,6 +18,8 @@ > #include <saNtf.h> > #include <stdlib.h> > #include "ntfsv_mem.h" > +#include "osaf_extended_name.h" > +#include "saAis.h" > #include <logtrace.h> > > void ntfsv_free_header(const SaNtfNotificationHeaderT > *notificationHeader) > @@ -27,10 +29,14 @@ void ntfsv_free_header(const SaNtfNotifi > free(notificationHeader->eventType); > if (notificationHeader->eventTime != NULL) > free(notificationHeader->eventTime); > - if (notificationHeader->notificationObject != NULL) > + if (notificationHeader->notificationObject != NULL) { > + osaf_extended_name_free(notificationHeader->notificationObject); > free(notificationHeader->notificationObject); > - if (notificationHeader->notifyingObject != NULL) > + } > + if (notificationHeader->notifyingObject != NULL) { > + osaf_extended_name_free(notificationHeader->notifyingObject); > free(notificationHeader->notifyingObject); > + } > if (notificationHeader->notificationClassId != NULL) > free(notificationHeader->notificationClassId); > if (notificationHeader->notificationId != NULL) @@ -486,10 +492,10 > @@ void ntfsv_copy_ntf_header(SaNtfNotifica > *(dest->eventTime) = *(src->eventTime); > > /* Set Notification Object */ > - *(dest->notificationObject) = *(src->notificationObject); > + ntfsv_sanamet_copy(dest->notificationObject, > src->notificationObject); > > /* Set Notifying Object */ > - *(dest->notifyingObject) = *(src->notifyingObject); > + ntfsv_sanamet_copy(dest->notifyingObject, src->notifyingObject); > > /* set Notification Class Identifier */ > dest->notificationClassId->vendorId = > src->notificationClassId->vendorId; > @@ -954,7 +960,7 @@ SaAisErrorT ntfsv_filter_header_alloc(Sa > /* Notification objects */ > if (numNotificationObjects != 0) { > > - header->notificationObjects = (SaNameT > *)malloc(numNotificationObjects * sizeof(SaNameT)); > + header->notificationObjects = (SaNameT > *)calloc(numNotificationObjects, sizeof(SaNameT)); > if (header->notificationObjects == NULL) { > TRACE_1("Out of memory notificationObjects"); > rc = SA_AIS_ERR_NO_MEMORY; > @@ -964,7 +970,7 @@ SaAisErrorT ntfsv_filter_header_alloc(Sa > > /* Notifying objects */ > if (numNotifyingObjects != 0) { > - header->notifyingObjects = (SaNameT > *)malloc(numNotifyingObjects * > sizeof(SaNameT)); > + header->notifyingObjects = (SaNameT > *)calloc(numNotifyingObjects, > sizeof(SaNameT)); > if (header->notifyingObjects == NULL) { > TRACE_1("Out of memory notifyingObjects"); > rc = SA_AIS_ERR_NO_MEMORY; > @@ -1175,9 +1181,9 @@ error_free: > void ntfsv_filter_header_free(SaNtfNotificationFilterHeaderT > *header) > { > free(header->eventTypes); > - free(header->notificationObjects); > - free(header->notifyingObjects); > - free(header->notificationClassIds); > + free(header->notificationObjects); > + free(header->notifyingObjects); > + free(header->notificationClassIds); > } > > void > ntfsv_filter_sec_alarm_free(SaNtfSecurityAlarmNotificationFilterT > *s_filter) > @@ -1226,3 +1232,130 @@ void ntfsv_filter_attr_ch_free(SaNtfAttr > } > } > > +/** > + * @Brief: Copy SaNameT pointed by pSrc to pDes > + * > + */ > +SaAisErrorT ntfsv_sanamet_copy(SaNameT* pDes, SaNameT* pSrc) { > + SaAisErrorT rc = SA_AIS_OK; > + if (osaf_is_an_extended_name(pDes)) { > + TRACE("Can not modify the existing longDn object"); > + return SA_AIS_ERR_EXIST; > + } > + if (osaf_is_an_extended_name(pSrc)) { > + osaf_extended_name_alloc(osaf_extended_name_borrow(pSrc), pDes); > + } else { > + /* pSrc is old SaNameT format, @.length could be counted with or > + * without null-termination, we need to preserve the @.length, > + * just a memcpy here > + */ > + memcpy(pDes, pSrc, sizeof(SaNameT)); > + } > + return rc; > +} > + > +/** > + * @Brief: Check SaNameT is a valid formation > + * > + */ > +bool ntfsv_sanamet_is_valid(const SaNameT* pName) { > + if (!osaf_is_extended_name_valid(pName)) { > + LOG_ER("Environment variable SA_ENABLE_EXTENDED_NAMES " > + "is not set, or not using extended name api"); > + return false; > + } > + if (osaf_extended_name_length(pName) > kMaxDnLength) { > + LOG_ER("Exceeding maximum of extended name length(%u)" > + ,kMaxDnLength); > + return false; > + } > + return true; > +} > + > +/** > + * @Brief: Clone the string pointer in extended name > + * > + */ > +void ntfsv_sanamet_clone_strptr(SaNameT* pName) { > + if (osaf_is_an_extended_name(pName)) > + osaf_extended_name_alloc(osaf_extended_name_borrow(pName), > pName); > > +} > + > +/** > + * @Brief: Return the length of string specified in SaNameT type. > + * > + */ > +size_t ntfs_sanamet_length(const SaNameT* pName) { > + if (osaf_is_an_extended_name(pName)) > + return osaf_extended_name_length(pName); > + > + /* In case of unextended name, sometimes @.length includes the count > on > + * null-termination, and osaf_extended_name_length returns the > length > + * excluding the null-termination which less 1 than the @.length. > Here > + * we need the length specified in @.length. This case mainly > applies > + * in encodeSaNameT/decodeSaNameT in order to preserve the original > + * @.length value > + */ > + size_t length = *((SaUint16T*)pName); > + osafassert(length < SA_MAX_UNEXTENDED_NAME_LENGTH); > + return length; > +} > + > +/** > + * @Brief: Allocate the new null-terminated string in SaNameT type, > and return > + * the pointer of this new string. > + * > + */ > +SaStringT ntfs_sanamet_strdup(SaNameT* pName) { > + SaStringT pRtr; > + SaConstStringT pStr; > + size_t length; > + > + length = osaf_extended_name_length(pName) + 1; > + pStr = osaf_extended_name_borrow(pName); > + > + pRtr = (char*) malloc(length); > + if (pRtr == NULL) { > + LOG_ER("out of memory"); > + return NULL; > + } > + memcpy(pRtr, pStr, length); > + pRtr[length - 1] = '\0'; > + return pRtr; > +} > + > +/** > + * @Brief: Create SaNameT input by string @value and @length, > + * @value is not freed then > + */ > +void ntfs_sanamet_alloc(SaConstStringT value, size_t length, SaNameT* > pName) > +{ > + osaf_extended_name_alloc(value, pName); > + /* Accept the old SaNameT which's @.length counting the null > termination */ > + if (!osaf_is_an_extended_name(pName) > + && ((ntfs_sanamet_length(pName)+1) == length)) { > + *((SaUint16T*)pName) += 1; > + } > +} > + > +/** > + * @Brief: Create SaNameT input by string @value and @length, > + * @value is freed then > + */ > +void ntfs_sanamet_steal(SaStringT value, size_t length, SaNameT* > pName) > +{ > + /* create pName by string @value */ > + osaf_extended_name_free(pName); > + osaf_extended_name_steal(value, pName); > + > + /* Accept the old SaNameT which's @.length counting the null > termination */ > + if (!osaf_is_an_extended_name(pName) > + && ((ntfs_sanamet_length(pName)+1) == length)) { > + *((SaUint16T*)pName) += 1; > + } > +} ------------------------------------------------------------------------------ Want fast and easy access to all the code in your enterprise? Index and search up to 200,000 lines of code with a free copy of Black Duck Code Sight - the same software that powers the world's largest code search on Ohloh, the Black Duck Open Hub! Try it now. http://p.sf.net/sfu/bds _______________________________________________ Opensaf-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/opensaf-devel
