0

I'm going to be getting in data from a file of my own making. This file will contain a printf format string and the parameters passed to it. I've already generated this code.

Now I want to do the reverse. Read format string and the parameters and pass it back to printf functions. Can I somehow generate the appropriate call stack or am I going to have to reparse the format string and send it to printf() piecemeal?

Edit

I know the risks with the printf functions. I understand that there are security vulnerabilities. These issues are non-issues as:

  1. This is to be used in a debugging context. Not to be handled outside of that scope.
  2. Executable that reads the file, is executed by the person who made the file.
  3. The datafile created will be read by an executable that simply expands the file and is not accessible by a third party.
  4. It has no access to writing anything to memory (%n is not valid).

Use Case

To compress a stream with minimal CPU overhead by tracking constantly repeating strings and replacing them with enumerations. All other data is saved as binary data, thus requiring only minimal processing instead of having to convert it to a large string every time.

Community
  • 1
  • 1
Adrian
  • 10,246
  • 4
  • 44
  • 110
  • Neither. `snprintf()` is prehistoric C. Modern C++ code uses the streams library, that implements formatted output in a fundamentally different way. If someone's intent is to learn modern C++, they should not be wasting their time on old-style C formatting. – Sam Varshavchik Nov 23 '18 at 02:50
  • related: https://stackoverflow.com/q/150543/1132334 – Cee McSharpface Nov 23 '18 at 02:50
  • This also creates a pretty glaring security vulnerability. – Peter Ruderman Nov 23 '18 at 02:52
  • @PeterRuderman, the security vulnerability is negligible as it is for local logging and not for public consumption. – Adrian Nov 23 '18 at 02:55
  • @PeterRuderman, for a means to output faster to a stream, it is worth it. And you're right, the `va_list` doesn't appear to be the way. I jumped the gun on that. – Adrian Nov 23 '18 at 03:04
  • @PeterRuderman, do you have a safer alternative to increase throughput while only marginally impacting performance? This is _strictly for logging_ in a _debugging_ context, allowing to replace constantly repeating strings into enumerations. The _risk_ is non-existent. However, if you have a better suggestion, I'm listening. – Adrian Nov 23 '18 at 03:14
  • 1
    It is true that streams have higher overhead than C-style stdio formatting. However, with modern, multi-Ghz CPUs, this only becomes an issue when generating huge amounts of output. Any difference in "throughput" between streams and C-style stdio formatting is mostly academic, otherwise. And, with C++ streams you get type-safety. The best way to avoid writings bugs is to make it logically impossible to create them; and type-safety makes many classes of bugs logically impossible. – Sam Varshavchik Nov 23 '18 at 03:39
  • Thanks @dlatikay, but unfortunately, although related, it doesn't have information that would help. There was one, but it looks to be extremely fragile. – Adrian Nov 23 '18 at 03:40
  • @SamVarshavchik, There is a lot of data that is being logged. By narrowing the scope of the problem, I can drop the possibility of any problem to near (if not) zero. So I don't need your lecturing. ty This ain't my first rodeo. – Adrian Nov 23 '18 at 03:43
  • then what about [this](https://stackoverflow.com/q/29618457/1132334)? – Cee McSharpface Nov 23 '18 at 03:47
  • If there is a maximum bound on the number of arguments and types, you could consider template meta programming. It may explode your executable size out however. – Gregory Currie Nov 23 '18 at 03:49
  • @GregroyCurrie, you've got my curiosity peaked. I couldn't think of a way to use templates in a domain that is essentially dynamic. – Adrian Nov 23 '18 at 03:50
  • @dlatikay, thanks again. That has possibilities, but still relies on fiddly bits which I would rather be kept to the library. I guess I'm stuck just parsing the format strings manually and translating each format specifier one at a time. – Adrian Nov 23 '18 at 04:06
  • @adrian You're right, if it's dynamic to the point where it's unbound, it's impossible, but you can do if for n number of parameters, where n is a preset upper bound. – Gregory Currie Nov 23 '18 at 04:22
  • Thanks @GregroyCurrie, I think I might just do a manual parse. It prolly won't be that much slower, if at all, and speed really doesn't matter too much as that isn't as important than outputting the data file faster. It is just more work. – Adrian Nov 23 '18 at 04:23
  • You might like to look at this SO question: https://stackoverflow.com/questions/11695237/creating-va-list-dynamically-in-gcc-can-it-be-done – Paul Sanders Nov 23 '18 at 14:33

0 Answers0