Check for strnlen and provide it if it is not found #5340

pull paveljanik wants to merge 2 commits into bitcoin:master from paveljanik:strnlen changing 4 files +29 −0
  1. paveljanik commented at 11:16 AM on November 21, 2014: contributor

    strnlen is POSIX.1-2008. Thus on some older/incompatible systems, this function is not defined. E.g. current mingw (see http://sourceforge.net/p/mingw/bugs/1912/) or on older Mac OS X. Check for such conditions and use memchr based implementation instead. This should fix #5338.

  2. laanwj added the label Build system on Nov 21, 2014
  3. laanwj commented at 12:22 PM on November 21, 2014: member

    In the mingw bug report there is also a free-standing version given: http://sourceforge.net/p/mingw/bugs/1912/?page=1 I'd feel safer using that if we can't rely on memchr being correct either.

    static __inline__ size_t strnlen( const char *start, size_t maxlen )
    {
      /* Determine the length of a NUL terminated string, subject
       * to a maximum permitted length constraint.
       */
      const char *stop = start;
    
      /* Scan at most maxlen bytes, seeking a NUL terminator;
       * note that we MUST enforce the length check, BEFORE the
       * terminator check, otherwise we could scan maxlen + 1
       * bytes, which POSIX forbids.
       */
      while( ((stop - start) < maxlen) && *stop )
        ++stop;
    
      /* Result is the number of non-NUL bytes actually scanned.
       */
      return stop - start;
    }
    

    BTW @cfields is util.cpp the right place for compatibility functions, or do we have a better place for those?

  4. paveljanik commented at 12:26 PM on November 21, 2014: contributor

    I'd prefer getting rid of strnlen completely... Writing such backwards compatible stuff can only bring in new issues. And in this case, both environments have their solutions (fork of mingw, updated Mac OS X/Xcode)...

  5. jonasschnelli commented at 12:28 PM on November 21, 2014: contributor

    i agree with @paveljanik. I don't think support for old or non-posix system should end up in core code.

  6. laanwj commented at 12:34 PM on November 21, 2014: member

    @paveljanik so you're proposing that we just close this? Fine with me, too.

  7. paveljanik commented at 12:43 PM on November 21, 2014: contributor

    @laanwj I'd like to see the original problem fixed somehow ;) In this PR or elsewhere. The previous check before strnlen was OK. Is it acceptable for you to revert it and use "if" check as before?

  8. laanwj commented at 12:45 PM on November 21, 2014: member

    No, it is not acceptable to revert it. The old code was incorrect.

    As it seems, strnlen is only not supported on old and broken platforms, so IMO we should just keep it this way.

  9. paveljanik commented at 12:50 PM on November 21, 2014: contributor

    @laanwj we could revert it intelligently, of course ;)

  10. laanwj commented at 12:58 PM on November 21, 2014: member

    No. Using POSIX functions should be ok.

    Either we add a compatibility fallback for strnlen, or we don't, but that code is going to stay the same. I don't want to change everything down to the lowest common denominator of broken platforms.

  11. paveljanik commented at 1:16 PM on November 21, 2014: contributor

    @laanwj Agreed. So I think the compatibility fallback is the way to go (I really would like to see Bitcoin Core compilable there). @cfields I also do not like util.h/cpp for this. Maybe under compat/ somewhere/new files?

  12. paveljanik commented at 4:43 PM on November 21, 2014: contributor
  13. theuni commented at 6:28 PM on November 21, 2014: member

    Agreed that somewhere in compat/ is better. We probably have a few other things like this (atoi64 as a quick bad example) that we could justify moving to a new dependency-less compat file.

  14. theuni commented at 6:48 PM on November 21, 2014: member

    @paveljanik I like that approach. A separate file just for that is a bit overkill, but it also ensures that unrelated junk won't end up there in the future. I don't mind either way.

  15. paveljanik commented at 6:54 PM on November 21, 2014: contributor

    @theuni Yes, having one file for one function is not optimal. Can you come up with a better name for it? ;-) misc_functions.cpp and waiting for atoi64 and others?

  16. laanwj commented at 8:57 AM on November 24, 2014: member

    @paveljanik ACK. I like that approach. A function per file may be overkill, but it's better than stuffing everything in util.cpp. We can always group them later if there are a lot and it makes sense. Can you push that to this branch?

  17. Check for strnlen and provide it if it is not found. 494f6e7d35
  18. paveljanik force-pushed on Nov 24, 2014
  19. paveljanik commented at 9:35 AM on November 24, 2014: contributor

    @ laanwj Done

  20. paveljanik commented at 9:43 AM on November 24, 2014: contributor

    @LinuXperia can you please verify with the standard mingw please?

  21. jgarzik commented at 2:32 PM on November 24, 2014: contributor

    ut ACK

  22. theuni commented at 5:32 PM on November 24, 2014: member

    Being nitpicky: It looks like compat.h needs to include config/bitcoin-config.h. We've probably gotten away with it only by accident (include ordering) in the past.

  23. theuni commented at 5:37 PM on November 24, 2014: member

    Also being nitpicky: if the intention here is to avoid lumping things together needlessly.. this now pulls compat.h in as a dependency for anything that requires strnlen. compat/strnlen.h would avoid that, if necessary.

  24. paveljanik commented at 6:05 PM on November 24, 2014: contributor

    @theuni yes, compat.h needs bitcoin-config.h. Even now, because there is:

    // As Solaris does not have the MSG_NOSIGNAL flag for send(2) syscall, it is defined as 0
    #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
    #define MSG_NOSIGNAL 0
    #endif
    

    already. strnlen.h means, that everyone using strnlen needs to include it. I do not want that ;-)

  25. Include missing config/bitcoin-config.h. c8ed6130a4
  26. paveljanik commented at 7:20 AM on November 26, 2014: contributor

    @theuni bitcoin-config.h added. OK now?

  27. theuni commented at 7:57 AM on November 26, 2014: member

    Thanks. utACK.

  28. laanwj commented at 8:43 AM on November 26, 2014: member

    [Strange - this only worked for me after manually rm src/bitcoin-config.h. Seemingly this file moved to src/config and my build was still picking up the old file]

    For google-ability it was failing with the following error:

    In file included from util.h:17:0,
                     from chainparamsbase.cpp:8:
    compat.h:92:8: error: previous declaration of ‘size_t strnlen(const char*, size_t)’ with ‘C++’ linkage
     size_t strnlen( const char *start, size_t max_len);
            ^
    In file included from /usr/include/c++/4.8/cstring:42:0,
                     from /usr/include/boost/filesystem/path.hpp:36,
                     from util.h:27,
                     from chainparamsbase.cpp:8:
    /usr/include/string.h:407:74: error: conflicts with new declaration with ‘C’ linkage
          __THROW __attribute_pure__ __nonnull ((1));
                                                                              ^
    /usr/include/string.h:407:74: error: declaration of ‘size_t strnlen(const char*, size_t) throw ()’ has a different exception specifier
    In file included from util.h:17:0,
                     from chainparamsbase.cpp:8:
    compat.h:92:8: error: from previous declaration ‘size_t strnlen(const char*, size_t)’
     size_t strnlen( const char *start, size_t max_len);
            ^
    

    Problem is unrelated to this pull specifically though.

    ACK commithash c8ed6130a4f7cfae897220ec093b440d21727cde https://dev.visucore.com/bitcoin/acks/5340

  29. paveljanik commented at 8:50 AM on November 26, 2014: contributor

    Isn't this because of this?

    crypto/common.h:#include "bitcoin-config.h" netbase.cpp:#include "bitcoin-config.h"

  30. laanwj merged this on Nov 26, 2014
  31. laanwj closed this on Nov 26, 2014

  32. laanwj referenced this in commit 70f9e33fa0 on Nov 26, 2014
  33. theuni commented at 8:52 AM on November 26, 2014: member

    @laanwj Very strange. bitcoin-compat.h was moved to a new path to avoid problems like that where a stale file could be found. I'm not following your compiler's logic, since util.h explicitly includes "config/bitcoin-config.h".

    Unless it's a linker error due to netbase.cpp, that'd be different. (that one should be fixed either way).

  34. laanwj commented at 8:53 AM on November 26, 2014: member

    No, it was not a linker error.

  35. theuni commented at 8:54 AM on November 26, 2014: member

    @paveljanik the first one is correct, the second should be fixed. crypto/ is setup to not have access to core headers by default, and uses -Iconfig/ so that it can read only config/bitcoin-config.h

  36. paveljanik deleted the branch on Nov 27, 2014
  37. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-22 09:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me