No strcpy either

75 points | by firesteelrain 2 hours ago

30 comments

  • t43562 40 minutes ago

    I've always wondered at the motivatons of the various string routines in C - every one of them seems to have some huge caveat which makes them useless.

    After years I now think it's essential to have a library which records at least how much memory is allocated to a string along with the pointer.

    Something like this: https://github.com/msteinert/bstring

      formerly_proven 27 minutes ago

      strncpy is fairly easy, that's a special-purpose function for copying a C string into a fixed-width string, like typically used in old C applications for on-disk formats. E.g. you might have a char username[20] field which can contain up to 20 characters, with unused characters filled with NULs. That's what strncpy is for. The destination argument should always be a fixed-size char array.

      A couple years ago we got a new manual page courtesy of Alejandro Colomar just about this: https://man.archlinux.org/man/string_copying.7.en

        Cyph0n 9 minutes ago

        strncpy doesn’t handle overlapping buffers (undefined behavior). Better to use strncpy_s (if you can) as it is safer overall. See: https://en.cppreference.com/w/c/string/byte/strncpy.html.

        As an aside, this is part of the reason why there are so many C successor languages: you can end up with undefined behavior if you don’t always carefully read the docs.

        ufo 23 minutes ago

        A big footgun with strncpy is that the output string may not be null terminated.

          kccqzy 16 minutes ago

          Yeah but fixed width strings don’t need null termination. You know exactly how long the string is. No need to find that null byte.

            ninkendo 6 minutes ago

            Until you pass them as a `char *` by accident and it eventually makes its way to some code that does expect null termination.

            There’s languages where you can be quite confident your string will never need null termination… but C is not one of them.

            Sharlin 3 minutes ago

            Good luck though remembering not to pass one to any function that does expect to find a null terminator.

  • swinglock an hour ago

    I'm surprised curlx_strcopy doesn't return success. Sure you could check if dest[0] != '/0' if you care to, but that's not only clumsy to write but also error prone, and so checking for success is not encouraged.

      jutter 15 minutes ago

      This is especially bizarre given that he explains above that "it is rare that copying a partial string is the right choice" and that the previous solution returned an error...

      So now it silently fails and sets dest to an empty string without even partially copying anything!?

      AlexeyBrin 40 minutes ago

      I guess the idea is that if the code does not crash at this line:

          DEBUGASSERT(slen < dsize);
      
      it means it succeeded. Although some compilers will remove the assertions in release builds.

      I would have preferred an explicit error code though.

  • Scubabear68 an hour ago

    From the article:

    > It has been proven numerous times already that strcpy in source code is like a honey pot for generating hallucinated vulnerability claims

    This closing thought in the article really stood out to me. Why even bother to run AI checking on C code if the AI flags strcpy() as a problem without caveat?

      Sharlin a minute ago

      Because these people who run AI checks on OSS code and submit bogus bug reports either assume that AIs don't make mistakes, or just don't care.

      CGamesPlay an hour ago

      It's not quite as black and white as the article implies. The hallucinated vulnerability reports don't flag it "without caveat", they invent a convoluted proof of vulnerability with a logical error somewhere along the way, and then this is what gets submitted as the vulnerability report. That's why it's so agitating for the maintainers: it requires reading a "proof" and finding the contradiction.

      saagarjha an hour ago

      Because people are stupid and use AI for things it is not good at.

        Tempest1981 an hour ago

        > people are stupid

        people overestimate AI

  • pama 2 hours ago

    Congrats on the completion of this effort! C/C++ can be memory safe but take some effort.

    IMHO the timeline figure could benefit in mobile from using larger fonts. Most plotting libraries have horrible font size defaults. I wonder why no library picked the other extreme end: I have never seen too large an axis label yet.

      Tempest1981 38 minutes ago

      Yes, the graph font-sizes seem intended for printing them on a single sheet of paper, vs squeezed into a single column in a blog.

      saagarjha an hour ago

      Removing strcpy from your code does not make it memory safe.

  • loeg 41 minutes ago

    A weird Annex-K like API. The destination buffer size includes space for the trailing nul, but the source size only includes non-nul string bytes.

    I don't really think this adds anything over forcing callers to use memcpy directly, instead of strcpy.

  • stabbles an hour ago

    Apart from Daniel Sternberg's frequent complaints about AI slop, he also writes [1]

    > A new breed of AI-powered high quality code analyzers, primarily ZeroPath and Aisle Research, started pouring in bug reports to us with potential defects. We have fixed several hundred bugs as a direct result of those reports – so far.

    [1] https://daniel.haxx.se/blog/2025/12/23/a-curl-2025-review/

  • TZubiri 25 minutes ago

    LMAO

    After all this time the initial AI Slop report was right:

    https://hackerone.com/reports/2298307

  • snvzz an hour ago

    The AI chatbot vulnerability reports part sure is sad to read.

    Why is this even a thing and isn't opt-in?

    I dread the idea of starting to get notifications from them in my own projects.

      trollbridge an hour ago

      Making a strcpy honeypot doesn’t sound like a bad idea…

        void nobody_calls_me(const char *stuff) {
                char *a, *b;
                const size_t c = 1024;
      
                a = calloc(c);
                if (!a) return;
                b = malloc(c);
                if (!b) {
                        free(a);
                        return;
                }
                strncpy(a, stuff, c - 1);
                strcpy(b, a);
                strcpy(a, b);
                free(a);
                free(b);
        }
      
      Some clever obfuscation would make this even more effective.
      Y_Y an hour ago

      Because humans generate and relay the slop-reports in the hopes of being helpful

        captn3m0 an hour ago

        s/being helpful/making money.

  • senthil_rajasek 2 hours ago

    Title is :

    No strcpy either

    @dang

      Snild an hour ago

      I don't see a problem with that, but for the record, the title on the site is lower-case for me (both browser tab title, and the header when in reader mode).

        1f60c 32 minutes ago

        I think the submission originally had a typo ("strpy", with no C)