mirror of
				https://xff.cz/git/u-boot/
				synced 2025-10-31 02:15:45 +01:00 
			
		
		
		
	lib: string: Fix strlcpy return value
strlcpy should always return the number of bytes copied. We were
accidentally missing the nul-terminator. We also always used to return a
non-zero value, even if we did not actually copy anything.
Fixes: 23cd138503 ("Integrate USB gadget layer and USB CDC driver layer")
Signed-off-by: Sean Anderson <seanga2@gmail.com>
			
			
This commit is contained in:
		
							
								
								
									
										12
									
								
								lib/string.c
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								lib/string.c
									
									
									
									
									
								
							| @@ -114,17 +114,21 @@ char * strncpy(char * dest,const char *src,size_t count) | |||||||
|  * NUL-terminated string that fits in the buffer (unless, |  * NUL-terminated string that fits in the buffer (unless, | ||||||
|  * of course, the buffer size is zero). It does not pad |  * of course, the buffer size is zero). It does not pad | ||||||
|  * out the result like strncpy() does. |  * out the result like strncpy() does. | ||||||
|  |  * | ||||||
|  |  * Return: the number of bytes copied | ||||||
|  */ |  */ | ||||||
| size_t strlcpy(char *dest, const char *src, size_t size) | size_t strlcpy(char *dest, const char *src, size_t size) | ||||||
| { | { | ||||||
| 	size_t ret = strlen(src); |  | ||||||
|  |  | ||||||
| 	if (size) { | 	if (size) { | ||||||
| 		size_t len = (ret >= size) ? size - 1 : ret; | 		size_t srclen = strlen(src); | ||||||
|  | 		size_t len = (srclen >= size) ? size - 1 : srclen; | ||||||
|  |  | ||||||
| 		memcpy(dest, src, len); | 		memcpy(dest, src, len); | ||||||
| 		dest[len] = '\0'; | 		dest[len] = '\0'; | ||||||
|  | 		return len + 1; | ||||||
| 	} | 	} | ||||||
| 	return ret; |  | ||||||
|  | 	return 0; | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user