Just following the recommendations: "Set errno to zero before calling a library function known to set errno, and check errno only after the function returns a value indicating failure"
I think in general it is not a good idea to set errno after a library call. In this case it could be done, but other functions behave differently. See https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=6619179 -- Oriol Arcas Software Engineer Starflow Networks On Thu, Apr 13, 2017 at 8:55 PM, Maxim Uvarov <maxim.uva...@linaro.org> wrote: > Why not just set errno to 0 in the success case? > > On 04/13/17 17:05, Oriol Arcas wrote: > > Fixes bug #2921. > > > > During odp_shm_reserve() mmap is tried with huge pages, and if it fails, > > without huge pages. The first call leaves errno set, which can confuse > > the client app. > > > > In general, errno must be cleared before calling any library call known > > to set it (see SEI CERT rule ERR30-C). > > > > Signed-off-by: Oriol Arcas <or...@starflownetworks.com> > > --- > > platform/linux-generic/_ishmphy.c | 5 +++++ > > 1 file changed, 5 insertions(+) > > > > diff --git a/platform/linux-generic/_ishmphy.c > b/platform/linux-generic/_ishmphy.c > > index d519af6..87c33ef 100644 > > --- a/platform/linux-generic/_ishmphy.c > > +++ b/platform/linux-generic/_ishmphy.c > > @@ -121,10 +121,12 @@ void *_odp_ishmphy_map(int fd, void *start, > uint64_t size, > > * The initial free maping can then be removed. > > */ > > mapped_addr = MAP_FAILED; > > + errno = 0; > > mapped_addr_tmp = mmap(NULL, size, PROT_READ | PROT_WRITE, > > MAP_SHARED | mmap_flags, fd, 0); > > if (mapped_addr_tmp != MAP_FAILED) { > > /* If OK, do new map at right fixed location... */ > > + errno = 0; > > mapped_addr = mmap(start, > > size, PROT_READ | PROT_WRITE, > > MAP_SHARED | MAP_FIXED | > mmap_flags, > > @@ -132,11 +134,13 @@ void *_odp_ishmphy_map(int fd, void *start, > uint64_t size, > > if (mapped_addr != start) > > ODP_ERR("new map failed:%s\n", > strerror(errno)); > > /* ... and remove initial mapping: */ > > + errno = 0; > > if (munmap(mapped_addr_tmp, size)) > > ODP_ERR("munmap failed:%s\n", > strerror(errno)); > > } > > } else { > > /* just do a new mapping in the VA space: */ > > + errno = 0; > > mapped_addr = mmap(NULL, size, PROT_READ | PROT_WRITE, > > MAP_SHARED | mmap_flags, fd, 0); > > if ((mapped_addr >= common_va_address) && > > @@ -154,6 +158,7 @@ void *_odp_ishmphy_map(int fd, void *start, uint64_t > size, > > /* if locking is requested, lock it...*/ > > if (flags & _ODP_ISHM_LOCK) { > > if (mlock(mapped_addr, size)) { > > + errno = 0; > > if (munmap(mapped_addr, size)) > > ODP_ERR("munmap failed:%s\n", > strerror(errno)); > > ODP_ERR("mlock failed:%s\n", strerror(errno)); > > > >