Talk:C Programming/C Reference/nonstandard/strlcpy

Isn't there an error in the example? memcpy is only called if length is greater than stack_buffer, i.e. the copy remains empyt

Original: if (length < sizeof(stack_buffer)) { // it fit, use the stack buffer copy = stack_buffer; } else { // it was truncated, use a slower buffer on the heap: copy = malloc(length+1); if (copy != NULL) memcpy(copy, source, length+1); }

Suggestion: if (length < sizeof(stack_buffer)) { // it fit, use the stack buffer copy = stack_buffer; } else { // it was truncated, use a slower buffer on the heap: copy = malloc(length+1); } if (copy != NULL) memcpy(copy, source, length+1);