Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-4679

Prefer strerror_l instead of dealing with buggy XSI-compliant strerror_r detection

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 1.25.0, 1.24.3
    • Affects Version/s: None
    • Component/s: libbson
    • Labels:
      None

      Update: Resolution

      Reduced the scope of strerror_r use to the Apple platform where strerror_l is not yet available and deliberately opted to discard the return value to avoid dealing with complicated XSI-compliance detection. Prefer strerror_l otherwise on Unix-like platforms as to-be-recommended by POSIX spec. See the PR description for more detail.

      Summary

      A user in mongodb/mongo-php-driver#1445 reported a build error compiling libbson (via the PHP driver) on Alpine Linux:

      /tmp/t/php-src/ext/mongodb/src/libmongoc/src/libbson/src/bson/bson-error.c:113:8: error: incompatible integer to pointer conversion assigning to 'char *' from 'int' [-Wint-conversion]
         ret = strerror_r (err_code, buf, buflen);
             ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      1 error generated.
      

      According to the clang docs, -Wint-conversion is an error by default. Since libbson is tested with clang, I think this has more to do with Alpine linux. Some further research lead me to strerror_r(3):

      The XSI-compliant version of strerror_r() is provided if:

      (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE

      Otherwise, the GNU-specific version is provided.

      Looking at bson-error.c, libbson currently uses the following condition to check for GNU:

      defined(__GNUC__) && defined(_GNU_SOURCE)

      If that condition is not met, XSI is used by default.

      Environment

      User was compiling the PHP driver 1.16.1 with bundled libbson 1.24.1 using clang on Alpine Linux (versions unknown).

      The PHP driver was being compiled statically into PHP, which is atypical, but I don't think that's relevant.

      Additional Background

      Some related discussions on strerror_r(3) and Alpine:

            Assignee:
            ezra.chung@mongodb.com Ezra Chung
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: