[CDRIVER-4679] Prefer strerror_l instead of dealing with buggy XSI-compliant strerror_r detection Created: 13/Jul/23  Updated: 28/Nov/23  Resolved: 24/Jul/23

Status: Closed
Project: C Driver
Component/s: libbson
Affects Version/s: None
Fix Version/s: 1.25.0, 1.24.3

Type: Bug Priority: Unknown
Reporter: Jeremy Mikola Assignee: Ezra Chung
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by PHPC-2263 Detect XSI or GNU strerror_r() implem... Closed
Related
related to PHPC-2332 Fix bson_strerror_r compatibility for... Closed
related to CDRIVER-4790 AIX has a glibc-compatible strerror_r... Closed
related to PHPC-2270 Define constants for POSIX features a... Closed

 Description   

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:



 Comments   
Comment by Ezra Chung [ 04/Aug/23 ]

jmikola@mongodb.com Done.

Comment by Jeremy Mikola [ 04/Aug/23 ]

ezra.chung@mongodb.com: Would you like to rename this issue for changelog visibility reasons? Perhaps something to convey we're preferring strerror_l for XSI, falling back to GNU strerror_r, and erroring otherwise? The special Windows/Apple logic doesn't need to be mentioned IMO.

Comment by Githook User [ 03/Aug/23 ]

Author:

{'name': 'Ezra Chung', 'email': '88335979+eramongodb@users.noreply.github.com', 'username': 'eramongodb'}

Message: CDRIVER-4679 Revert BSON_HAVE_XSI_STRERROR_R and use strerror_l instead (#1370)

  • Revert commit ac436db8c1c0ee45a663a469e0783d50713e16e3.
Comment by Githook User [ 03/Aug/23 ]

Author:

{'name': 'Ezra Chung', 'email': '88335979+eramongodb@users.noreply.github.com', 'username': 'eramongodb'}

Message: CDRIVER-4679 Revert BSON_HAVE_XSI_STRERROR_R and use strerror_l instead (#1370)

  • Revert commit ac436db8c1c0ee45a663a469e0783d50713e16e3.
Comment by Githook User [ 24/Jul/23 ]

Author:

{'name': 'Ezra Chung', 'email': '88335979+eramongodb@users.noreply.github.com', 'username': 'eramongodb'}

Message: CDRIVER-4679 Prefer XSI-compliant strerror_r when available (#1350)

  • CDRIVER-4679 Prefer XSI-compliant strerror_r when available
Comment by Githook User [ 24/Jul/23 ]

Author:

{'name': 'Ezra Chung', 'email': '88335979+eramongodb@users.noreply.github.com', 'username': 'eramongodb'}

Message: CDRIVER-4679 Prefer XSI-compliant strerror_r when available (#1350)

  • CDRIVER-4679 Prefer XSI-compliant strerror_r when available
Comment by Jeremy Mikola [ 14/Jul/23 ]

Some further investigation by the user determined that php/php-src@067df26 (PHP 8.2+) introduced -D_GNU_SOURCE to PHP's build flags. That explains why the issue only started appearing in PHP 8.2+.

I believe libbson still needs a patch to improve its detection of which strerror_r() variant is being used.

I also noted that PHP calls strerror_r() once, but never works with its return value. That might explain why PHP itself never encountered a build error; however, there may still be a lingering bug there for the GNU version of the function, which I reported in this comment on the above commit.

Generated at Wed Feb 07 21:21:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.