views:

99

answers:

1

I have functions that I wish to call based on some input. Each function has different number of arguments. In other words,

if (strcmp(str, "funcA") == 0) funcA(a, b, c);
else if (strcmp(str, "funcB") == 0) funcB(d);
else if (strcmp(str, "funcC") == 0) funcC(f, g);

This is a bit bulky and hard to maintain. Ideally, these are variadic functions (e.g., printf-style) and can use varargs. But they are not. So exploiting the cdecl calling convention, I am stuffing the stack via a struct full of parameters. I'm wondering if there's a better way to do it. Note that this is strictly for in-house (e.g., simple tools, unit tests, etc.) and will not be used for any production code that might be subjected to malicious attacks.

Example:

#include <stdio.h>

typedef struct __params
{
    unsigned char* a;
    unsigned char* b;
    unsigned char* c;
} params;

int funcA(int a, int b)
{
    printf("a = %d, b = %d\n", a, b);
    return a;
}

int funcB(int a, int b, const char* c)
{
    printf("a = %d, b = %d, c = %s\n", a, b, c);
    return b;
}

int funcC(int* a)
{
    printf("a = %d\n", *a);
    *a *= 2;
    return 0;
}

typedef int (*f)(params);

int main(int argc, char**argv)
{
    int val;
    int tmp;
    params myParams;
    f myFuncA = (f)funcA;
    f myFuncB = (f)funcB;
    f myFuncC = (f)funcC;

    myParams.a = (unsigned char*)100;
    myParams.b = (unsigned char*)200;

    val = myFuncA(myParams);
    printf("val = %d\n", val);

    myParams.c = (unsigned char*)"This is a test";
    val = myFuncB(myParams);
    printf("val = %d\n", val);

    tmp = 300;
    myParams.a = (unsigned char*)&tmp;
    val = myFuncC(myParams);
    printf("a = %d, val = %d\n", tmp, val);
    return 0;
}

Output:

gcc -o func func.c
./func
a = 100, b = 200
val = 100
a = 100, b = 200, c = This is a test
val = 200
a = 300
a = 600, val = 0
+1  A: 

Rather than trying to torture this out of corner cases in the language, I would do this in a much more straightforward way, by defining variadic wrapper functions. You can do this generically with macros:

#define WRAP_VARIADIC_2_ARG(FNAME, RETTYPE, ARGTYPE1, ARGTYPE2) \
  (RETTYPE) wrapped_##FNAME(...) {                              \
    va_list args;                                               \
    va_start(args, 2);                                          \
    (ARGTYPE1) arg1 = va_arg(args, (ARGTYPE1));                 \
    (ARGTYPE2) arg2 = va_arg(args, (ARGTYPE2));                 \
    va_end(args);                                               \
    return FNAME(arg1, arg2);                                   \
  }

And similar macros for other arg counts. Then, you call:

WRAP_VARIADIC_2_ARG(funcA, int, int, int)
WRAP_VARIADIC_3_ARG(funcB, int, int, int, const char*)
WRAP_VARIADIC_1_ARG(funcC, int, int*)

This will define a set of functions with the following signatures that you can use in your dispatching function:

int wrapped_funcA(...)
int wrapped_funcB(...)
int wrapped_funcC(...)

This should be straightforward from there.

Brooks Moses