Bug #118927 Incorrect Binary Library Hex Representation Due to Sign Extension in SHOW CREATE LIBRARY
Submitted: 2 Sep 21:41 Modified: 18 Sep 23:07
Reporter: Akshat Nehra (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Parser Severity:S3 (Non-critical)
Version:9.4 OS:Any
Assigned to: CPU Architecture:ARM (arm64)

[2 Sep 21:41] Akshat Nehra
Description:
The SHOW CREATE LIBRARY command produces an incorrect hexadecimal representation of binary libraries on x86_64 architectures. On x86_64 platforms (where char is signed by default), the command inserts erroneous "FFFFFF" patterns before each byte with the high bit set, resulting in an invalid and bloated hex representation.

For example, on x86_64:
AS 0x3872FFFFFFD3FFFFFFBBFFFFFFA66A27FFFFFFA9FFFFFFA34F3834FFFFFFF3FFFFFF82FFFFFFBC1A7360FFFFFFD3FFFFFFBA0C5A7761FFFFFFE5FFFFFFE67A7B

While on AArch64:
AS 0x3872D3BBA66A27A9A34F3834F382BC1A7360D3BA0C5A7761E5E67A7B

The AArch64 representation is actually correct, as each byte is properly represented by exactly 2 hex digits, which is the standard way to represent binary data in hexadecimal format. On x86_64 (where char is signed by default), negative byte values undergo sign extension, resulting in "FFFFFF" patterns in the hex representation. On AArch64 (where char is unsigned by default), these patterns are absent.

This inconsistency causes test failures across different architectures and produces misleading, non-standard hexadecimal representations.

How to repeat:
Run the MySQL Test Run (MTR) test main.sp-library-binary on both x86_64 and AArch64 architectures:

```
[ 50%] main.sp-library-binary                    [ fail ]
        Test ended at 2025-09-02 20:34:16

CURRENT_TEST: main.sp-library-binary
--- /quick-mysql-build/mysql-server/mysql-test/r/sp-library-binary.result	2025-09-02 23:18:41.432449980 +0300
+++ /quick-mysql-build/build/mysql-test/var/log/sp-library-binary.reject	2025-09-02 23:34:14.844343433 +0300
@@ -44,7 +44,7 @@
 binary_library_binary	ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION	CREATE LIBRARY `binary_library_binary`
     COMMENT 'A binary library stored in binary encoding'
     LANGUAGE WASM
-AS 0x3872FFFFFFD3FFFFFFBBFFFFFFA66A27FFFFFFA9FFFFFFA34F3834FFFFFFF3FFFFFF82FFFFFFBC1A7360FFFFFFD3FFFFFFBA0C5A7761FFFFFFE5FFFFFFE67A7B
+AS 0x3872D3BBA66A27A9A34F3834F382BC1A7360D3BA0C5A7761E5E67A7B
```

The test fails on AArch64 because it expects the incorrect x86_64 representation with "FFFFFF" patterns, but AArch64 correctly produces a standard hex representation.

Suggested fix:
The issue occurs in the binary_to_hex function in sp.cc. The recommended solution is to prevent sign extension on all platforms by using a bit mask to isolate only the relevant 8 bits of each byte:
This approach masks off the higher bits with & 0xFF, preventing sign extension regardless of platform. It would produce the more compact and correct representation (matching current AArch64 behavior) on all architectures.

This solution produces the standard hexadecimal representation where each byte is represented by exactly 2 hex digits, which is the universally accepted format for binary data. It would require updating the existing test result to match the correct behavior.

An alternative solution would be to cast each byte to unsigned char first:
This fix explicitly casts each byte to unsigned char first, then to unsigned int, ensuring no sign extension occurs on any platform. This produces consistent output that matches the correct AArch64 behavior. This too also require updating the existing test result to match the correct behavior.
[2 Sep 22:18] Akshat Nehra
Pull Request: https://github.com/mysql/mysql-server/pull/628
[3 Sep 10:12] MySQL Verification Team
Thank you very much for your patch contribution, we appreciate it!

In order for us to continue the process of reviewing your contribution to MySQL, please send us a signed copy of the Oracle Contributor Agreement (OCA) as outlined in https://oca.opensource.oracle.com

Signing an OCA needs to be done only once and it's valid for all other Oracle governed Open Source projects as well.

Getting a signed/approved OCA on file will help us facilitate your contribution - this one, and others in the future.  

Please let me know, if you have any questions.

Thank you for your interest in MySQL.
[18 Sep 19:52] OCA Admin
Contribution submitted via Github - Bug#118927 Fix incorrect hex representation in SHOW CREATE LIBRARY 
(*) Contribution by Akshat Nehra (Github akshatnehra, mysql-server/pull/628#issuecomment-3308536018): I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: git_patch_2794087547.txt (text/plain), 2.76 KiB.