Skip to contentSkip to conversation

terminate readlink result string

PATCH Applied Tobias Stoeckmann 2 participants 2 messages Jul 11, 2015 – Jul 13, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 terminate readlink result string

readlink does not guarantee that its result string is nul-terminated. Instead, increase the buffer by one byte to make sure that we can add '\0' at the end.

---
 alsactl/init_sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
alsactl/init_sysfs.c +2 -2
index 0cbada2..5c789b6 100644
@@ -108,11 +108,11 @@ static char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
 
 	if (S_ISLNK(statbuf.st_mode)) {
 		/* links return the last element of the target path */
-		char link_target[PATH_SIZE];
+		char link_target[PATH_SIZE + 1];
 		int len;
 		const char *pos;
 
-		len = readlink(path_full, link_target, sizeof(link_target));
+		len = readlink(path_full, link_target, sizeof(link_target) - 1);
 		if (len > 0) {
 			link_target[len] = '\0';
 			pos = strrchr(link_target, '/');
Signature · 1 line
2.4.5
Applied
#1 lore raw
Quoted 4 lines · Tobias Stoeckmann

readlink does not guarantee that its result string is nul-terminated. Instead, increase the buffer by one byte to make sure that we can add '\0' at the end.

Applied, thanks.

Takashi

Quoted 30 lines
---
 alsactl/init_sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
alsactl/init_sysfs.c +2 -2
index 0cbada2..5c789b6 100644
@@ -108,11 +108,11 @@ static char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
 
 	if (S_ISLNK(statbuf.st_mode)) {
 		/* links return the last element of the target path */
-		char link_target[PATH_SIZE];
+		char link_target[PATH_SIZE + 1];
 		int len;
 		const char *pos;
 
-		len = readlink(path_full, link_target, sizeof(link_target));
+		len = readlink(path_full, link_target, sizeof(link_target) - 1);
 		if (len > 0) {
 			link_target[len] = '\0';
 			pos = strrchr(link_target, '/');
Signature · 6 lines
2.4.5

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel