Skip to contentSkip to conversation

x86/cpu/intel: replace deprecated strcpy with strscpy

PATCH Superseded Ruben Wauters 2 participants 5 messages May 7, 2025 – May 15, 2025 1 message not in the archive x86

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 x86/cpu/intel: replace deprecated strcpy with strscpy

strcpy is deprecated due to lack of bounds checking.
This patch replaces strcpy with strscpy, the recommended alternative for null terminated strings, to follow best practices.

---
 arch/x86/kernel/cpu/intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/intel.c +1 -1
index 584dd55bf739..b49bba30434d 100644
@@ -607,7 +607,7 @@ static void init_intel(struct cpuinfo_x86 *c)
 		}
 
 		if (p)
-			strcpy(c->x86_model_id, p);
+			strscpy(c->x86_model_id, p);
 	}
 #endif
 
Signature · 1 line
2.48.1
#2 lore raw
Quoted 14 lines · Ruben Wauters

strcpy is deprecated due to lack of bounds checking.
This patch replaces strcpy with strscpy, the recommended alternative for null terminated strings, to follow best practices.

---
arch/x86/kernel/cpu/intel.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/intel.c +0 -0
index 584dd55bf739..b49bba30434d 100644

arch/x86/kernel/cpu/intel.c static void init_intel(struct cpuinfo_x86 *c) · line 613

Quoted 8 lines
arch/x86/kernel/cpu/intel.c +1 -1
@@ -607,7 +607,7 @@ static void init_intel(struct cpuinfo_x86 *c)
 		}
 
 		if (p)
-			strcpy(c->x86_model_id, p);
+			strscpy(c->x86_model_id, p);
 	}
 #endif
 

strscpy() needs a buffer length; this patch wouldn't even compile!

Not to mention that the string in question is generated in such a way that cannot be unterminated.

#3 lore raw
Quoted 20 lines · H. Peter Anvin
Quoted 18 lines · Ruben Wauters

strcpy is deprecated due to lack of bounds checking. This patch replaces strcpy with strscpy, the recommended alternative for
null terminated strings, to follow best practices.

---
arch/x86/kernel/cpu/intel.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/intel.c +0 -0
index 584dd55bf739..b49bba30434d 100644
@@ -607,7 +607,7 @@ static void init_intel(struct cpuinfo_x86 *c)
 		}
 
Quoted 8 lines

arch/x86/kernel/cpu/intel.c static void init_intel(struct cpuinfo_x86 *c) · line 613

Quoted 6 lines
arch/x86/kernel/cpu/intel.c +1 -1
 		if (p)
-			strcpy(c->x86_model_id, p);
+			strscpy(c->x86_model_id, p);
 	}
 #endif
 

strscpy() needs a buffer length; this patch wouldn't even compile!

Hi, this is incorrect, strscpy is defined in string.h as

#define strscpy(dst, src, ...)	\
	CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src,

__VA_ARGS__)

the third parameter is optional, and it works perfectly fine with two parameters. I have compiled it, and there are no errors.

Quoted 2 lines

Not to mention that the string in question is generated in such a way that cannot be unterminated.

I'm not entirely sure what you mean here? The assignments above are null terminated strings, which the two parameter version works fine with.

#4 lore raw
Quoted 38 lines · Ruben Wauters
Quoted 20 lines · H. Peter Anvin
Quoted 18 lines · Ruben Wauters

strcpy is deprecated due to lack of bounds checking. This patch replaces strcpy with strscpy, the recommended alternative for
null terminated strings, to follow best practices.

---
arch/x86/kernel/cpu/intel.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/intel.c +0 -0
index 584dd55bf739..b49bba30434d 100644
@@ -607,7 +607,7 @@ static void init_intel(struct cpuinfo_x86 *c)
 		}
 
Quoted 8 lines

arch/x86/kernel/cpu/intel.c static void init_intel(struct cpuinfo_x86 *c) · line 613

Quoted 6 lines
arch/x86/kernel/cpu/intel.c +1 -1
 		if (p)
-			strcpy(c->x86_model_id, p);
+			strscpy(c->x86_model_id, p);
 	}
 #endif
 

strscpy() needs a buffer length; this patch wouldn't even compile!

Hi, this is incorrect, strscpy is defined in string.h as

#define strscpy(dst, src, ...)	\
	CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src,

__VA_ARGS__)

the third parameter is optional, and it works perfectly fine with two parameters. I have compiled it, and there are no errors.

Quoted 7 lines
Quoted 3 lines

Not to mention that the string in question is generated in such a way
that cannot be unterminated.

I'm not entirely sure what you mean here? The assignments above are null terminated strings, which the two parameter version works fine with.

Hello

Just wanted to check that everything was ok with this patch, and that any concerns were addressed or explained. Please do let me know if there is anything I need to do or change to get this patch applied.

Ruben Wauters

#5 lore raw
Quoted 47 lines · Ruben Wauters
Quoted 38 lines · Ruben Wauters
Quoted 20 lines · H. Peter Anvin
Quoted 18 lines · Ruben Wauters

strcpy is deprecated due to lack of bounds checking. This patch replaces strcpy with strscpy, the recommended alternative for
null terminated strings, to follow best practices.

---
arch/x86/kernel/cpu/intel.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/intel.c +0 -0
index 584dd55bf739..b49bba30434d 100644
@@ -607,7 +607,7 @@ static void init_intel(struct cpuinfo_x86 *c)
 		}
 
Quoted 8 lines

arch/x86/kernel/cpu/intel.c static void init_intel(struct cpuinfo_x86 *c) · line 613

Quoted 6 lines
arch/x86/kernel/cpu/intel.c +1 -1
 		if (p)
-			strcpy(c->x86_model_id, p);
+			strscpy(c->x86_model_id, p);
 	}
 #endif
 

strscpy() needs a buffer length; this patch wouldn't even compile!

Hi, this is incorrect, strscpy is defined in string.h as

#define strscpy(dst, src, ...)	\
	CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src,

__VA_ARGS__)

the third parameter is optional, and it works perfectly fine with two parameters. I have compiled it, and there are no errors.

Quoted 7 lines
Quoted 3 lines

Not to mention that the string in question is generated in such a way
that cannot be unterminated.

I'm not entirely sure what you mean here? The assignments above are null terminated strings, which the two parameter version works fine with.

Quoted 8 lines

Hello

Just wanted to check that everything was ok with this patch, and that any concerns were addressed or explained. Please do let me know if there is anything I need to do or change to get this patch applied.

Ruben Wauters

Yes, I stand corrected.

I still think it is superfluous (or arguably a memcpy would be better, since this is a fixed length) but it doesn't hurt enything.