Home > Software design >  Does sprint(buf, "%s", buf 10) where strlen(buf 10)>10 count as overlapping?
Does sprint(buf, "%s", buf 10) where strlen(buf 10)>10 count as overlapping?

Time:03-02

I know I should use memmove if there's overlapping but I can't imagine how sprintf (or memcpy for that matter) would be coded such that it would run into a problem.

CodePudding user response:

The source and target are clearly overlapping. So, yes, this counts.

CodePudding user response:

The C Standard is clear, so you should not make any assumptions regarding how memcpy could possibly be implemented. Imagine an architecture where memory can be read a written out of order more efficiently than sequentially. Implementors of memcpy could take advantage of the non-overlapping constraint to issue CPU instructions that move multiple words at a time in indeterminate order, thus potentially overwriting source bytes before they are read.

  • Related