A macro that copies string u to string v and then returns the unrelated value e? And it doesn't null-terminate v properly? I'm not very experienced with C; does this actually serve a purpose without breaking, and if so, what does it do?
Its a panic macro part of a much larger function, this function depends on copying part of a string onto itself (this is why there's no termination) and this macro simply reverts changes and returns an error code so it can be called inplace of return.
I use the scope for variable saftey, I just prefer to use a semicolon otherwise it looks like an outlier which can get quite distracting when looking for something else.
I do not wrap macro when no variable are déclared, but when I wrap them, I wrap with {} and when I call the macro I put a useless semicolon, I find this more readeable than a do while(0)
It keeps all variables defined within that scope isolated to that scope, also means that I can define arguments that may already be in other places without having to worry about it crashing due to a broken type. Its just a good practice to avoid issues with macros in general.
For crying out loud. Get thee off to ConfidentlyIncorrect, and learn the basics of C (in particular, the effect of putting an extra semicolon ahead of anelse).
I think what he means by "And it doesn't null-terminate v properly?" is that when you use strlen, then the value it returns is the length without the final null terminator strlen("test\0")==4, and since the for loop uses < u_s instead of <= u_s it will not copy the null terminator to the other string, making this a segfault casino. Also if the length of u is larger than v you end up with problems.
I don't know "_plib_strlen", but even on the off-chance that it includes the trailing \0 terminator in the length count (quite odd, that), it's still really weird to then see the defensive \0-char-assignment to v[0]. More likely it's not copying the trailing \0. It's a bit weird to copy a string except the trailing \0, but it's even weirder to copy a string except the trailing \0 except when it's empty, and then DO copy that trailing \0.
i.e. the macro kind of makes sense then - it _is_ counting the string length including the 0 terminator, with a special case returning 0 when the string pointer is itself null. That just means the copy function projects both an empty and missing "u" string to the empty "v" output.
I would love clarification on "copying part of a string onto itself". Are you suggesting that u is a pointer to some location in a string and v is a pointer to the beginning of that string? (Or at least some location prior to u.) So the point is to copy everything from u to the end of the string to an earlier part of the string? I'm struggling to imagine a situation where that is valuable
The macro is used only where v and u are strictly the same size. the reason we dont null terminate v is because u already has a null terminator that is copyed over to v.
If u and v are the same size, then they are either different strings with the same length or they are the same string, right? I thought "copy onto itself" implied they point to the same contiguous non-nil character sequence
This is the least readable font I've seen in my life, especially with the colour and highlighting. I genuinely had to scan parts of the first line letter-by-letter to read them. Deliberate eye strain?
The compiler shortens them anyway, even if i had longer names that would take up DISK SPACE on the filesize not memory as in RAM. Also an abstraction of your statement, memory at the moment is very very expensive.
The real horror (other than the font) is the majority of commenters lack of insight into why this might exist, and why it’s written in the rule bending way that it is.
C is really becoming a lost art. Especially C in different programming regimes, where the “rules” may be different than in vanilla “safe” application programming.
Always returns whatever e is (well, if it doesn't crash first).
Copies string u to v horridly because:
if u is empty, it puts a null terminator
if u is not empty, it doesn't put the null terminator (< instead of <=)
if v is not big enough or if either is null, then fuck you (well plib may check for null and return 0... but)
the strlen doesn't have a max, so if u is not properly null terminated, you put whatever memory you get after u until the first zero byte... it can get long
113
u/AMathMonkey Jan 27 '26
A macro that copies string u to string v and then returns the unrelated value e? And it doesn't null-terminate v properly? I'm not very experienced with C; does this actually serve a purpose without breaking, and if so, what does it do?
14
u/CrownLikeAGravestone Jan 27 '26
This is the least readable font I've seen in my life, especially with the colour and highlighting. I genuinely had to scan parts of the first line letter-by-letter to read them. Deliberate eye strain?
6
u/IllustratorFar127 Jan 27 '26
You do realize memory is basically free and you can have longer variable names, right?
3
u/timmerov Jan 28 '26
it gets you transferred to the remedial coders team.
182
u/git0ffmylawnm8 Jan 27 '26
the real horror here is the eyefuck of a font
30
u/joshuakb2 Jan 27 '26
Buffer overflow for some reason?
4
u/BoredOfReposts Jan 27 '26
The real horror (other than the font) is the majority of commenters lack of insight into why this might exist, and why it’s written in the rule bending way that it is.
C is really becoming a lost art. Especially C in different programming regimes, where the “rules” may be different than in vanilla “safe” application programming.
OP, you’re my kind of coder.
23
u/VisualSome9977 Jan 27 '26
the way this font is rendered makes it look more like the lethal company ship terminal than anything I would ever want to look at all day
7
u/KCGD_r Jan 27 '26
the font looks like you're trying to remember what the code looks like
12
u/Symbroson [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Jan 27 '26 edited Jan 27 '26
copies a string but without its null terminator and returns a value for some reason Also causing a buffer overflow if used carelessly
6
u/sirkubador Jan 27 '26
Always returns whatever e is (well, if it doesn't crash first).
Copies string u to v horridly because:
👌
2
u/Grandpa_P1g Jan 27 '26
We are not in space my boi what is this font
5
u/OscarElmahdy Jan 27 '26
It causes emotional distress and eye damage
3
u/mathisntmathingsad Jan 28 '26
The real horror is the font
2
u/Braydenley9 Jan 27 '26
Write a program.
2
u/Splatpope Jan 27 '26
strains the eye, mostly
2
u/t3kner Jan 27 '26
Is that font named "obfuscation"?
2
u/Sir_Bebe_Michelin Jan 28 '26
Looks like an automaton font
Very undemocratic
2
u/MichiganDogJudge Feb 02 '26
It returns the value of e that was passed to it. The do body is executed, but only once. e is never modified.
2
u/DeathByThousandCats Jan 27 '26
Some nutcase wanted to "implement" a destructor in C macro? But why?
605
u/[deleted] Jan 27 '26
[removed] — view removed comment