NFSIISE icon indicating copy to clipboard operation
NFSIISE copied to clipboard

Implement platform independent solution for vaargs and va_list in printf/scanf

Open JayFoxRox opened this issue 5 years ago • 0 comments

Currently, vaargs (... as parameter) and va_list seem to be platform dependent; I'm surprised the current approach works at all.

This primarily (only?) affects the following code:

https://github.com/zaps166/NFSIISE/blob/fd87de061df72c40c76772405c0a45666059eafe/src/Wrapper.c#L706-L709

https://github.com/zaps166/NFSIISE/blob/fd87de061df72c40c76772405c0a45666059eafe/src/Wrapper.c#L710-L718

The solution would be to manually implement these functions, so the arguments are popped correctly.

I've prototyped a solution:

REALIGN int32_t vsprintf_wrap(char *s, const char *fmt, va_list arg)
{
  uint32_t* args = (uint32_t*)arg;
  
  if (!strcmp(fmt, "LOW %-8s")) {
    char* a1 = args[0];
    return sprintf(s, fmt, a1);
  } else if (!strcmp(fmt, "HIGH %-7s")) {
    char* a1 = args[0];
    return sprintf(s, fmt, a1);
  } else {
    printf("Unhandled '%s'\n", fmt);
  }
  assert(false);
  return 0;
}

So this is similar how other functions are implemented (expecting specific input). However, it would also be possible to search for % (or full formatting options like %-8s) and parse only each format argument.

JayFoxRox avatar Mar 14 '19 19:03 JayFoxRox