1

In order to implement a client to some protocol that expects null-byte padding to a specific string, I implemented a functions that pads a string with a specific amount of null bytes:

string padToFill(string s, int fill){
    int len = s.length();
    if (len < fill){
        for (int i = 0; i < fill - len; i++){
            s.append("\0");
        }
    }
    return s;
}

However, in practice, the result I am getting from this function is identical to the string I am getting as argument. So for example padToFill("foo", 5) is foo and not foo\0\0. Even tho c++ strings are expected to handle null bytes as any other character.

How do I achieve the desired null padding?

7
  • 2
    how do you inspect the string ? A std::string can have many \0 at the end, but there are some pitfalls that could lead you to believe that there are none. The code you posted looks ok. What is the code you used to see how many null terminators are in the result? Commented Nov 8, 2022 at 13:44
  • 2
    s.push_back('\0'); Note that std::string already manages a \0-terminator at the end of the string, which is not part of the length. If you push back additional \0-terminators, those will be part of the length (and there will still be the additional not counted \0-terminated caboose). Commented Nov 8, 2022 at 13:44
  • @ring0 As for me then I have understood nothing. Commented Nov 8, 2022 at 13:47
  • sorry I was too fast with saying the code is fine. It isnt. Anyhow, adding how you know that the function doesnt work would be good Commented Nov 8, 2022 at 13:47
  • You are calling this function: string& append (const char* s); so you are appending a c string... Commented Nov 8, 2022 at 13:49

1 Answer 1

11

The append overload you're using accepts a C string, which is null-terminated (that is: the first null-byte marks its end).

The easiest way here is probably to use a different overload of append:

  s.append(fill - len, '\0');

Note that this requires C++20. For older C++ standards, there's

   s.resize(fill, '\0');

or just

   s.resize(fill);

Come to think of it, this may be a clearer way to describe what you're doing, anyway. I like the second option here for clarity.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.