Stubbing standard function - ambiguous call

Hi,
I need to make a stub for some standard library function, but the compiler gets confused about which function I want to call in my code. I thought that putting the stubbed function together with its caller into an annonymous namespace will give higher precedence to the stub, but it didn't.
Here is an example code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <signal.h>

namespace
{
int sigaction(int signum, const struct sigaction * act, struct sigaction * oldact)
{
  return -1;
}

void test()
{
  struct sigaction action;
  if ( 0 != sigaction( SIGPIPE, &action, NULL ) )
  {
      printf("Error\n");
      abort();
  }
}

}
int main()
{
  test();
  return 0;
}

Result:
main.cpp: In function ‘void {anonymous}::test()’:
main.cpp:23:47: error: call of overloaded ‘sigaction(int, sigaction*, NULL)’ is ambiguous
main.cpp:23:47: note: candidates are:
main.cpp:8:5: note: int {anonymous}::sigaction(int, const sigaction*, sigaction*)
In file included from main.cpp:4:0:
/usr/include/signal.h:266:12: note: int sigaction(int, const sigaction*, sigaction*)


Could anyone tell me is it possible to force the compiler to use my stubbed version of sigaction() and if yes, how to do that? I am not allowed to modify test() function.

Thanks in advance!
Last edited on
try giving the namespace actually a name
The obvious, brute-force approach is to change how you link the program, so that it doesn't link against the library that contains the definition of the function you're trying to stub out.

In TDD, this is known as a "link seam". However, that's harder to do with standard library functions than it would be with a function you'd written, because you may need other functions from that library. You'd have to stub out every function you're using from that library. That could be a lot of work, or not much work, depending on how many functions you're using.

If you were allowed to modify test(), then my answer would be to pass in a callback function pointer, and maybe have that default to the standard lib sigaction().

Why can't you modify test() ?
try giving the namespace actually a name

But wouldn't that then involve having to modify test() ?

Or is there something clever you can do with "using" statements so that stub::sigaction() hides, or takes precedence over, the standard sigaction() ?
Last edited on
MikeyBoy wrote:
But wouldn't that then involve having to modify test() ?
No. Both sigaction() and test() reside in the same namespace. An anonymous namespace and the global namespace are accessed the same way hence it's ambiguous. You have to access test() differently. Like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <signal.h>

namespace X
{
int sigaction(int signum, const struct sigaction * act, struct sigaction * oldact)
{
  return -1;
}

void test()
{
  struct sigaction action;
  if ( 0 != sigaction( SIGPIPE, &action, NULL ) )
  {
      printf("Error\n");
      abort();
  }
}

}
int main()
{
  X::test();
  return 0;
}
Topic archived. No new replies allowed.