-2

When I try to run a linux strings main | grep SWVER I get the value of the string I want but also "null" printed out right before it. My main file is a binary I have built from the code below. When I run the linux command on the binary I will get "nullSWVER:v1.0.0" as my response. I cannot understand where this null is coming from.

int main()
{
    const char* APP_VERSION = "SWVER:v1.0.0";
    printf("%s", APP_VERSION);
    
}

Built main binary and then ran linux command strings main | grep SWVER on main binary

Expected: "SWVER:v1.0.0" Actual: "nullSWVER:v1.0.0"

  • 3
    `strings` prints contiguous sequences of printable characters it finds. There is nothing preventing the compiler from placing some bonus printable characters before your string. I'd say your expectation is not justified and you should fix the expectation. – teapot418 Apr 27 '23 at 14:36
  • Use RCS tags instead. That way you can use existing tools to extract the information. – Ted Lyngmo Apr 27 '23 at 14:37
  • This is probably an [XY Problem](https://xyproblem.info/). `strings` is doing exactly what it's supposed to, and there is no reason to expect anything particular about its output except that some *substring* will include the string you're looking for. If you need to obtain the string "SWVER:v1.0.0" from your binary, this is not the way to go. – user229044 Apr 27 '23 at 14:42

2 Answers2

2

The layout of strings in memory isn't documented or guaranteed in any way beyond the fact that the string will be there once the binary is loaded into memory.

It's quite plausible that APP_VERSION may simply be initialized with a pointer that points partway into the contiguous sequence of bytes nullSWVER:v1.0.0, which makes no difference to the visible behavior of your program, provided you don't invoke undefined behavior at some point. It's also quite likely that some subtle change to the compiler, libraries, or compile flags may change this layout unpredictably.

You shouldn't be relying on strings to parse a binary anyway; chances are you'll see tons of cruft like function prologues that resemble strings, random strings from libc, etc.

It's also an implementation detail of typical executable formats that the strings are readable (common implementations place pages of .text and .data sections page-for-page into the executable file), but nothing about C requires that executable images are readable like that.

If you want to store version information in your binaries, you'll have to do it in an implementation-specific manner. Windows has version resources; for ELF files I believe the only approach would be a .note section (or a section with some other custom name) and then reading it in a way that understands and respects sections, rather than just blindly scanning for strings.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

you are looking into a binary file and only the compiler knows, what other characters got to be located just before your fixed string with the version information. Thats not what strings is made for. It was created to safe your terminal from getting wierd things (ESC sequences etc.) by cating a binary. It was definitely NOT made for delivering reliable formatting.

Synopsis
  • 190
  • 10