msys2 / msys2/msys2-runtime

Standard C library function not linked correctly in simple tests project

Open
#90 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
240
Forks
60
Avg merge
2d 3h
Merged PRs (30d)
1

Description

The problem I'm having is building and linking against a library that I'm doing unit test on. This previously worked in msys2 for many years (though a previous release broke it in the same way and was subsequently fixed). The problem I'm observing is that some standard lib functions are not linking correctly into my application if used in the library under test. However, if the unit tests use that same function then it links correctly. The failure is repeatable on multiple computer.

The following tools are used:

  1. Windows 10+
  2. Msys2 - Installer build date - Feb 10, 2022
  3. Gcc v11.2.0
  4. binutils v2.37
  5. Cmake v3.22.1
  6. Cmocka v1.1.5
  7. lcov v1.13 (not used due to crash but would run and generate coverage report)

I've attached a simple project to illustrate the issue. The interesting thing is that the linking fails when the strncpy() call is in the library being unit tested. However, if we add a call to strncpy() to the unit test then the linker finds it in the library and correctly links the test.

The code under tests is located in the source files of relevance are:
./msys2_gcc_v11_bug.c //Source below:

void msys2_gcc_v11_string_linker_bug(char * folderPath, size_t folderPathLen, const char  * fname)
{
        folderPath[folderPathLen] = '/';
        strncpy(&folderPath[folderPathLen + 1], fname, strlen(fname));
}

./unit_test/test-msys2_gcc_v11_bug.c // Test function - note #if 0 which allows the linking to operate correct if enabled

static void msys2_gcc_v11_bug_test(void ** state)
{
        #define TEST_BASE_PATH_STR "LOGS/Example"
        const char * test_basePathStr = TEST_BASE_PATH_STR;

        /* Allocate a buffer big enough to contain the path plus the largest file name. */
        char nodePathStr[64];
        strcpy(nodePathStr, TEST_BASE_PATH_STR);

        /* Enable a use of strncpy() in the test and the function is correctly link
         * to the application. Leave this disabled and the application segfaults
         * when the call to strncpy in the msys_gcc_v11_bug.c code is made. */
        #if 0

        char temp[64];
        strncpy(temp, "FORCED string", 64);
        printf(temp);
        #endif

        const size_t nodePathStrLen = strlen(nodePathStr);
        msys2_gcc_v11_string_linker_bug(nodePathStr, nodePathStrLen, "test_file");


        #undef TEST_BASE_PATH_STR

}

msys2_gcc_v11_bug.zip

So far I have traced the symbols using objdump at each stage of the compilation and noted that the symbols present and weak after the initial compile of the library under test. I've also recompiled and reinstalled CMake using the msys2 gcc v11 tool chain to see if that made any difference and it did not.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the attached project, especially msys2_gcc_v11_bug.c and unit_test/test-msys2_gcc_v11_bug.c, and build it with the listed MSYS2, GCC, binutils, CMake, and Cmocka versions. Compare the link and runtime behavior with the #if 0 block disabled and enabled; done means the test links and runs correctly without the forced strncpy() call.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cmake
Domain
build-system, compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.