Messages in this thread | | | From | Linus Torvalds <> | Date | Fri, 16 Jul 2021 11:45:57 -0700 | Subject | Re: [GIT PULL] tracing: histogram fix and take 2 on the __string_len() marcros |
| |
On Fri, Jul 16, 2021 at 11:37 AM Steven Rostedt <rostedt@goodmis.org> wrote: > > > So how do you want this implemented? > > #define __assign_str_len(dst, src, len) \ > do { \ > strscpy(__get_str(dst), (src) ? (const char *)(src) : "(null)", len); \ > __get_str(dst)[len] = '\0';
What? That "__get_str(dst)[len] = '\0';" is pointless and wrong.
That's the _point_. strscpy() does the whole NUL termination correctly, in ways that strncpy() never ever did.
But I also want to know what the actual _semantics_ of the source is. Your "memcpy()" example implies that the source is always a fixed-size thing. In that case, maybe that's the rigth thing to do, and you should just create a real function for it.
So two choices:
(a) either just plain strscpy() works (or, if you want NUL padding, use strscpy_pad()).
(b) you have very odd source/destination semantics, and it should be its own function that explains it.
Note how in neither case is it ok to just make random inline code with no explanations for the odd crazy code. Make a function that actually describes what you want, documents it, and be done with it.
strncpy() is garbage. It should never be used in new code.
And random semantics that are undocumented and just implemented as a illegible mess in a header file is not ok either.
Linus
| |