Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

c - how to not null terminate a buffer?

Basically I want to know if there is a way to keep a null byte in a buffer?

Example:

#include <stdio.h>

int main()
{
    char buf[] = "hello therex00, Hi";
    printf("%s
", buf);

    return 0;
}

Unfortunately, if you compile above, the output you would get is hello there only (null terminated by x00). So is there there a way to keep the null byte in the stack and get the Hi after that too?

FYI, if you wanted to recommend to escape with \x00, I can't do that.

question from:https://stackoverflow.com/questions/65907742/how-to-not-null-terminate-a-buffer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

C strings by definition cannot incorporate NUL bytes, that is explicitly reserved as a terminating character. If you need a raw buffer that does support that then you can't use a C string. You will need to treat it differently. All of the C string functions starting with str are off-limits here.

printf with %s will of course terminate on the NUL byte. If you want to print the whole buffer then you need to use lower-level tools like fwrite.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...