Snaipe / Mimick

A KISS, cross-platform C mocking library
MIT License
152 stars 43 forks source link

`mmk_reset` fails when passing a mocked function #7

Open damienflament opened 5 years ago

damienflament commented 5 years ago

Environment

CPU architecture: x86_64 Operating system: GNU/Linux Mimick version: 0.3.0

CMake configuration

cmake_minimum_required(VERSION 3.13)

project(vostrod
        DESCRIPTION "Mimick sample"
        LANGUAGES C)

find_package(PkgConfig REQUIRED)

pkg_check_modules(MIMICK REQUIRED mimick)
include_directories(${MIMICK_INCLUDE_DIRS})
link_libraries(${MIMICK_LIBRARIES})

add_executable(mimick_sample mimick_sample.c)

Issue

Running the first README sample:

#include <stdlib.h>
#include <assert.h>
#include <mimick.h>

mmk_mock_define (malloc_mock, void *, size_t);

int main(void) {
    mmk_mock("malloc@self", malloc_mock);

    void *result = NULL;
    mmk_when(malloc(mmk_any(size_t)),
            .then_return = &result,
            .then_errno = ENOMEM);

    assert(malloc(42) == result && errno == ENOMEM);

    mmk_reset(malloc);
}

results in the following error:

> ./mimick_sample
mimick: Could not find GOT entry for function malloc@self.
zsh: abort (core dumped)  ./mimick_sample

Removing the call to mmk_reset prevents the error. Running the second README example (e.i. calling mmk_reset passing the mock object returned by mmk_mock) succeeds.