Post B6u8Y2lnQ0H1UTYkhU by mirabilos@toot.mirbsd.org
 (DIR) More posts by mirabilos@toot.mirbsd.org
 (DIR) Post #B6u6mFVodjtH9ijNFA by landley@mstdn.jp
       0 likes, 0 repeats
       
       I want a "C with optimizers not breaking semantics". There is no reason strcpy(s, s+3) shouldn't work (when the string is long enough) except asshole optimizers BREAK CRAP in the name of speed.Stoppit.
       
 (DIR) Post #B6u6mFhVwFFRk0CiTg by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley strcpy should not work.Either you have the length and it is safe, then you use memcpy, or you don’t, then you use strlcpy.
       
 (DIR) Post #B6u8ElSfXxvSJ6Q6To by landley@mstdn.jp
       0 likes, 0 repeats
       
       @mirabilos strlen() plus memcpy() traverses the memory twice. If I've advanced into a string to match its contents and want to delete everything before that in the string, strcpy(start, midpoint) is the correct function.If you don't trust strcpy() to work you don't trust strlen() to work either, come off it.And what ASAN was bitching about was memcpy() vs memmove() anyway.
       
 (DIR) Post #B6u8ElmANTWPHZXfs0 by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley then don’t do the strlen first, keep the length with you.
       
 (DIR) Post #B6u8Em9YyUEkS8UML2 by landley@mstdn.jp
       0 likes, 0 repeats
       
       @mirabilos The complaint is "optimizing" using 32 or 64 bit read/writes... potentially unaligned, potentially reading past the null terminator, and using special bullshit to find a null byte in the middle of a 64-bit register.Some horrible arm thing using floating point extensions was doing 128-bit versions of this a few years back and BROKE MY CODE (string constant at end of ELF), so I made smemcmp() https://codeberg.org/landley/toybox/src/branch/master/lib/lib.c#L1528But now ASAN hates strcpy shortening a string in place. Fuck 'em.
       
 (DIR) Post #B6u8JO6FpdWhIh8kt6 by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley and memcpy is UB for overlaps, yes
       
 (DIR) Post #B6u8OExTFQLiOcLUUC by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley for shortening strings in place, you use memmove
       
 (DIR) Post #B6u8T0L51rWCVGf0k4 by landley@mstdn.jp
       0 likes, 0 repeats
       
       @mirabilos you want to pull that bullshit in rust, go for it. I have no problem with programs written in rust, I have a problem with shoehorning a SECOND language (and glue layers) into projects written cleanly in one language. The rust developers are not "owed" every existing C project. Write a fucking new one, did you think Linux was the first go at a Unix kernel? Coherent had no Bell Labs code in 1979.
       
 (DIR) Post #B6u8T0lJSKVBocvxdA by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley how do you get to rust now? We’re both talking about C, aren’t we?
       
 (DIR) Post #B6u8Y2lnQ0H1UTYkhU by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley as is strcpy, btw (C99 §7.21.2.3(2)s2)
       
 (DIR) Post #B6u8cO8nu3VqvjOQPQ by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley and C89 §4.11.2.3
       
 (DIR) Post #B6u91BCr8B0q3O6rc8 by landley@mstdn.jp
       0 likes, 0 repeats
       
       @mirabilos UB means "an optimizer developer felt their speed up was more important than the obvious meaning of the language". (Or in the case of function arguments not working the same way as commas outside of parentheses, because Turbo C wanted the option to link with Turbo Pascal objects, despite fundamentally different string handling.)You're in the desert, there's nothing to do, name the horse.If the behavior is "undefined", define it. LP64 works great.
       
 (DIR) Post #B6u91BR2HSM4lMkBiS by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley I also want less UB in C. However, it’s there, and real-existing compilers rely on it. (In this case, the libc implementation relies on the caller not invoking UB.)And strcpy has “always” been undefined for overlapping objects. Trying to use it as such is like trying to use ^ as the pipe operator in POSIX sh because it used to work in Bourne sh.
       
 (DIR) Post #B6u98Y4UW8rYzmi8WW by landley@mstdn.jp
       0 likes, 0 repeats
       
       @mirabilos Why do you think I was complaining about the need to traverse the string twice to humor an "optimization"? (More operations on fewer cache lines is quite often a win.)
       
 (DIR) Post #B6u98YKnXVuHoML9wO by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley then write your own string shortening function instead of misusing a libc function then complaining(or, just move the pointer to the beginning…)
       
 (DIR) Post #B6u9nI6GnrBtKmyDzc by landley@mstdn.jp
       0 likes, 0 repeats
       
       @mirabilos And sizeof(char) technically didn't have to be 1 for the longest time.If the behavior is undefined, define it. Don't quote the standard at me, I read Herbert Schildt's terribly annotated version for fun as a teenager because I'd reinvented bytecode and was trying to write my own compiler (with raw and interpreted code pointers analogous to Turbo C's near and far pointers). I had a printout of the glibc 1.x man pages (in EMX) hand-sewed into a book with thread.
       
 (DIR) Post #B6u9nIP3g0DgH3lEHI by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley then, I guess, you should know?
       
 (DIR) Post #B6u9nIoa96dVYDhc3s by landley@mstdn.jp
       0 likes, 0 repeats
       
       @mirabilos https://web.archive.org/web/20260000000000*/https://landley.net/c99-draft.html only found it in 2019 but the file was there in 2006.
       
 (DIR) Post #B6uAr1wytDeGMJyMPA by landley@mstdn.jp
       0 likes, 0 repeats
       
       @mirabilos x ? : y was a gcc extension, which got picked up by intel's ICC and tinycc and eventually LLVM and I honestly don't remember whether it's in the standard or not, I use it and it works.If compiler extensions aren't off limits, "undefined behavior" is bullshit.
       
 (DIR) Post #B6uAr2CDyXqF7b6XAG by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley it’s in the queue for being voted on for inclusion, AFAIK
       
 (DIR) Post #B6uBs1uCdqIqSoiIMa by landley@mstdn.jp
       0 likes, 0 repeats
       
       @mirabilos if you move the pointer to the beginning you can't free the string later.
       
 (DIR) Post #B6uBs27fpl4v8b13MO by mirabilos@toot.mirbsd.org
       0 likes, 0 repeats
       
       @landley there are multiple ways around this, depending on your use cases and environments (keep track of that separately, use spans, use arena allocators, …), but you’ll probably know that