home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,817 of 243,242   
   David Brown to highcrew   
   Re: UB-free slice   
   06 Jan 26 09:15:00   
   
   From: david.brown@hesbynett.no   
      
   On 05/01/2026 22:36, highcrew wrote:   
   > Hello,   
   >   
   > Let `struct Buffer` be defined as:   
   >   
   >    struct Buffer { unsigned char *bytes; size_t length; };   
   >   
   > Then let's define the following function:   
   >   
   >    int buffer_append(struct Buffer *dst, const struct Buffer *src)   
   >    {   
   >      // checks on size etc etc, return non-zero on failure   
   >   
   >      memcpy(dst->bytes, src->bytes, length);   
   >      dst->bytes += length;   
   >      dst->length -= length;   
   >      return 0;   
   >    }   
   >   
   > In my understanding, if we call buffer_append(&x, &y) for x.bytes and   
   > y.bytes pointing to overlapping areas of the same array, we get UB by   
   > the first two parameters of memcpy being restrict-qualified pointers.   
   >   
   > I suppose the correct thing to do would be to replace memcpy with   
   > memmove, allegedly taking some performance penalty.   
   >   
   > Assuming that such penalty matters on the scale of things, what is   
   > the correct/recommended way of handing this situation?   
      
   Use memmove.  Your assumption about the performance penalty is   
   unwarranted - it should be negligible on any scale of things.  And even   
   if the performance penalty was big, "correct" is better than "fast"   
   every time.   
      
   You have a very strange definition for your "Buffer" type.  A "Buffer"   
   type that does not keep track of the start of the buffer, but only the   
   end point and the space remaining in the memory allocation, is not much   
   use as a buffer.  It might be useful as a "Buffer_End_Point" type, but   
   not "Buffer".   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca