How to prevent from memcpy, cause Heap Overlflow?

I’m following Stanford CS155 security lesson’s presentation to learn integer overflow. I learned today that memcpy() function may lead to overflow.
The presentation says, If I have a code something like below, second memcpy() function may overflow heap.

void  func( char *buf1, *buf2,    unsigned int len1, len2) {
  char temp[256];
  if (len1 + len2 > 256) {return -1}    // length check
  memcpy(temp, buf1, len1); // cat buffers
  memcpy(temp+len1, buf2, len2);
  do-something(temp);   // do stuff
}

How can I prevent overflow? What should I change in memcpy() so that it will not cause an overflow?

Read more here: Source link