Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JayFoxRox opened this issue Mar 14, 2019 · 0 comments
Labels

Comments

@JayFoxRox
Copy link

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:

NFSIISE/src/Wrapper.c

Lines 706 to 709 in fd87de0

REALIGN int32_t vsprintf_wrap(char *s, const char *fmt, va_list arg)
{
return vsprintf(s, fmt, arg);
}

NFSIISE/src/Wrapper.c

Lines 710 to 718 in fd87de0

REALIGN int32_t fscanf_wrap(FILE *f, const char *fmt, ...)
{
int ret;
va_list arg;
va_start(arg, fmt);
ret = vfscanf(f, fmt, arg);
va_end(arg);
return ret;
}

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants