Skip to contentSkip to conversation

ACPI: replace deprecated strncpy()

RFC PATCH In review Ahmed Salem 2 participants 5 messages Mar 30, 2025 – Mar 31, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover ACPI: replace deprecated strncpy()

strncpy() is deprecated. This patch series is split over two patches, the first of which annotates destinations with __nonstring, and the second replaces strncpy() with strtomem() in the ACPI_COPY_NAMESEG macro. Additionally, two replacements in drivers/acpi/acpica/tbfind.c:60 with memcpy().

This is an effort to avoid using deprecated interfaces, and potential compiler warnings in the future.

Ahmed Salem (2):
ACPI: mark ACPI_COPY_NAMESEG destinations with __nonstring attribute
ACPI: replace deprecated strncpy() with strtomem()

 drivers/acpi/acpica/acdebug.h                            | 2 +-
 drivers/acpi/acpica/tbfind.c                             | 4 ++--
 drivers/acpi/prmt.c                                      | 2 +-
 drivers/acpi/sysfs.c                                     | 4 ++--
 include/acpi/actbl.h                                     | 6 +++---
 include/acpi/actypes.h                                   | 2 +-
 tools/power/acpi/os_specific/service_layers/oslinuxtbl.c | 2 +-
 tools/power/acpi/tools/acpidump/apfiles.c                | 2 +-
 8 files changed, 12 insertions(+), 12 deletions(-)

base-commit: b3c623b9a94f7f798715c87e7a75ceeecf15292f

Signature · 1 line
2.47.2
#1 lore raw

RFC PATCH 1/2 ACPI: mark ACPI_COPY_NAMESEG destinations with __nonstring attribute

strncpy(), which ACPI_COPY_NAMESEG currently uses, is deprecated[1].

This patch is the first of two, ultimately replacing strncpy() with strtomem(), avoiding future compiler warnings about truncation.

[1] https://github.com/KSPP/linux/issues/90

---
 drivers/acpi/acpica/acdebug.h                            | 2 +-
 drivers/acpi/prmt.c                                      | 2 +-
 drivers/acpi/sysfs.c                                     | 4 ++--
 include/acpi/actbl.h                                     | 6 +++---
 tools/power/acpi/os_specific/service_layers/oslinuxtbl.c | 2 +-
 tools/power/acpi/tools/acpidump/apfiles.c                | 2 +-
 6 files changed, 9 insertions(+), 9 deletions(-)
drivers/acpi/acpica/acdebug.h +1 -1
index 911875c5a5f1..2b56a8178f43 100644
@@ -37,7 +37,7 @@ struct acpi_db_argument_info {
 struct acpi_db_execute_walk {
 	u32 count;
 	u32 max_count;
-	char name_seg[ACPI_NAMESEG_SIZE + 1];
+	char name_seg[ACPI_NAMESEG_SIZE + 1] __nonstring;
 };
 
 #define PARAM_LIST(pl)                  pl
drivers/acpi/prmt.c +1 -1
index e549914a636c..ca70f01c940c 100644
@@ -40,7 +40,7 @@ struct prm_buffer {
 };
 
 struct prm_context_buffer {
-	char signature[ACPI_NAMESEG_SIZE];
+	char signature[ACPI_NAMESEG_SIZE] __nonstring;
 	u16 revision;
 	u16 reserved;
 	guid_t identifier;
drivers/acpi/sysfs.c +2 -2
index a48ebbf768f9..a05d4032d4f1 100644
@@ -307,9 +307,9 @@ static struct kobject *hotplug_kobj;
 
 struct acpi_table_attr {
 	struct bin_attribute attr;
-	char name[ACPI_NAMESEG_SIZE];
+	char name[ACPI_NAMESEG_SIZE] __nonstring;
 	int instance;
-	char filename[ACPI_NAMESEG_SIZE+ACPI_INST_SIZE];
+	char filename[ACPI_NAMESEG_SIZE+ACPI_INST_SIZE] __nonstring;
 	struct list_head node;
 };
 
include/acpi/actbl.h +3 -3
index 451f6276da49..8aa60281e7db 100644
@@ -66,12 +66,12 @@
  ******************************************************************************/
 
 struct acpi_table_header {
-	char signature[ACPI_NAMESEG_SIZE];	/* ASCII table signature */
+	char signature[ACPI_NAMESEG_SIZE] __nonstring;	/* ASCII table signature */
 	u32 length;		/* Length of table in bytes, including this header */
 	u8 revision;		/* ACPI Specification minor version number */
 	u8 checksum;		/* To make sum of entire table == 0 */
-	char oem_id[ACPI_OEM_ID_SIZE];	/* ASCII OEM identification */
-	char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];	/* ASCII OEM table identification */
+	char oem_id[ACPI_OEM_ID_SIZE] __nonstring;	/* ASCII OEM identification */
+	char oem_table_id[ACPI_OEM_TABLE_ID_SIZE] __nonstring;	/* ASCII OEM table identification */
 	u32 oem_revision;	/* OEM revision number */
 	char asl_compiler_id[ACPI_NAMESEG_SIZE];	/* ASCII ASL compiler vendor ID */
 	u32 asl_compiler_revision;	/* ASL compiler version */
tools/power/acpi/os_specific/service_layers/oslinuxtbl.c +1 -1
index 9d70d8c945af..52026b9e389e 100644
@@ -19,7 +19,7 @@ ACPI_MODULE_NAME("oslinuxtbl")
 typedef struct osl_table_info {
 	struct osl_table_info *next;
 	u32 instance;
-	char signature[ACPI_NAMESEG_SIZE];
+	char signature[ACPI_NAMESEG_SIZE] __nonstring;
 
 } osl_table_info;
 
tools/power/acpi/tools/acpidump/apfiles.c +1 -1
index 13817f9112c0..5a39b7d9351d 100644
@@ -103,7 +103,7 @@ int ap_open_output_file(char *pathname)
 
 int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance)
 {
-	char filename[ACPI_NAMESEG_SIZE + 16];
+	char filename[ACPI_NAMESEG_SIZE + 16] __nonstring;
 	char instance_str[16];
 	ACPI_FILE file;
 	acpi_size actual;
Signature · 1 line
2.47.2
#4 lore raw

RFC PATCH 2/2 ACPI: replace deprecated strncpy() with strtomem()

In reply to Ahmed Salem

strncpy() is deprecated for NUL-terminated destination buffers[1].

Use strtomem() for ACPI_COPY_NAMESEG() as destinations are length-bounded, and not all of them require NUL termination.

Also replace strncpy() with memcpy() for copying header.oem_id and header.oem_table_id

[1] https://github.com/KSPP/linux/issues/90

---
 drivers/acpi/acpica/tbfind.c | 4 ++--
 include/acpi/actypes.h       | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/acpi/acpica/tbfind.c +2 -2
index 1c1b2e284bd9..35f72dffc250 100644
@@ -57,8 +57,8 @@ acpi_tb_find_table(char *signature,
 
 	memset(&header, 0, sizeof(struct acpi_table_header));
 	ACPI_COPY_NAMESEG(header.signature, signature);
-	strncpy(header.oem_id, oem_id, ACPI_OEM_ID_SIZE);
-	strncpy(header.oem_table_id, oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
+	memcpy(header.oem_id, oem_id, ACPI_OEM_ID_SIZE);
+	memcpy(header.oem_table_id, oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
 
 	/* Search for the table */
 
include/acpi/actypes.h +1 -1
index 80767e8bf3ad..b85dec57f00d 100644
@@ -522,7 +522,7 @@ typedef u64 acpi_integer;
 #define ACPI_COPY_NAMESEG(dest,src)     (*ACPI_CAST_PTR (u32, (dest)) = *ACPI_CAST_PTR (u32, (src)))
 #else
 #define ACPI_COMPARE_NAMESEG(a,b)       (!strncmp (ACPI_CAST_PTR (char, (a)), ACPI_CAST_PTR (char, (b)), ACPI_NAMESEG_SIZE))
-#define ACPI_COPY_NAMESEG(dest,src)     (strncpy (ACPI_CAST_PTR (char, (dest)), ACPI_CAST_PTR (char, (src)), ACPI_NAMESEG_SIZE))
+#define ACPI_COPY_NAMESEG(dest, src)     (strtomem (ACPI_CAST_PTR (char, (dest)), ACPI_CAST_PTR (char, (src))))
 #endif
 
 /* Support for the special RSDP signature (8 characters) */
Signature · 1 line
2.47.2
#2 lore raw

Re: [RFC PATCH 1/2] ACPI: mark ACPI_COPY_NAMESEG destinations with __nonstring attribute

In reply to Ahmed Salem
Quoted 9 lines · Ahmed Salem

strncpy(), which ACPI_COPY_NAMESEG currently uses, is deprecated[1].

This patch is the first of two, ultimately replacing strncpy() with strtomem(), avoiding future compiler warnings about truncation.

[1] https://github.com/KSPP/linux/issues/90

ACPICA material is primarily handled by the upstream ACPICA project on GitHub, so please avoid mixing ACPICA code changes with changes to the other code in one patch.

Also, ACPICA changes should first be submitted to upstream ACPICA, as indicated on this list for many times, see for instance:

https://lore.kernel.org/linux-acpi/CAJZ5v0gUDxrAn4W+Rf3ifjrg8Z9ZzTTOZjPFSSN5488mPqzXeA@mail.gmail.com/

Quoted 101 lines
---
 drivers/acpi/acpica/acdebug.h                            | 2 +-
 drivers/acpi/prmt.c                                      | 2 +-
 drivers/acpi/sysfs.c                                     | 4 ++--
 include/acpi/actbl.h                                     | 6 +++---
 tools/power/acpi/os_specific/service_layers/oslinuxtbl.c | 2 +-
 tools/power/acpi/tools/acpidump/apfiles.c                | 2 +-
 6 files changed, 9 insertions(+), 9 deletions(-)
drivers/acpi/acpica/acdebug.h +1 -1
index 911875c5a5f1..2b56a8178f43 100644
@@ -37,7 +37,7 @@ struct acpi_db_argument_info {
 struct acpi_db_execute_walk {
        u32 count;
        u32 max_count;
-       char name_seg[ACPI_NAMESEG_SIZE + 1];
+       char name_seg[ACPI_NAMESEG_SIZE + 1] __nonstring;
 };
 
 #define PARAM_LIST(pl)                  pl
drivers/acpi/prmt.c +1 -1
index e549914a636c..ca70f01c940c 100644
@@ -40,7 +40,7 @@ struct prm_buffer {
 };
 
 struct prm_context_buffer {
-       char signature[ACPI_NAMESEG_SIZE];
+       char signature[ACPI_NAMESEG_SIZE] __nonstring;
        u16 revision;
        u16 reserved;
        guid_t identifier;
drivers/acpi/sysfs.c +2 -2
index a48ebbf768f9..a05d4032d4f1 100644
@@ -307,9 +307,9 @@ static struct kobject *hotplug_kobj;
 
 struct acpi_table_attr {
        struct bin_attribute attr;
-       char name[ACPI_NAMESEG_SIZE];
+       char name[ACPI_NAMESEG_SIZE] __nonstring;
        int instance;
-       char filename[ACPI_NAMESEG_SIZE+ACPI_INST_SIZE];
+       char filename[ACPI_NAMESEG_SIZE+ACPI_INST_SIZE] __nonstring;
        struct list_head node;
 };
 
include/acpi/actbl.h +3 -3
index 451f6276da49..8aa60281e7db 100644
@@ -66,12 +66,12 @@
  ******************************************************************************/
 
 struct acpi_table_header {
-       char signature[ACPI_NAMESEG_SIZE];      /* ASCII table signature */
+       char signature[ACPI_NAMESEG_SIZE] __nonstring;  /* ASCII table signature */
        u32 length;             /* Length of table in bytes, including this header */
        u8 revision;            /* ACPI Specification minor version number */
        u8 checksum;            /* To make sum of entire table == 0 */
-       char oem_id[ACPI_OEM_ID_SIZE];  /* ASCII OEM identification */
-       char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];      /* ASCII OEM table identification */
+       char oem_id[ACPI_OEM_ID_SIZE] __nonstring;      /* ASCII OEM identification */
+       char oem_table_id[ACPI_OEM_TABLE_ID_SIZE] __nonstring;  /* ASCII OEM table identification */
        u32 oem_revision;       /* OEM revision number */
        char asl_compiler_id[ACPI_NAMESEG_SIZE];        /* ASCII ASL compiler vendor ID */
        u32 asl_compiler_revision;      /* ASL compiler version */
tools/power/acpi/os_specific/service_layers/oslinuxtbl.c +1 -1
index 9d70d8c945af..52026b9e389e 100644
@@ -19,7 +19,7 @@ ACPI_MODULE_NAME("oslinuxtbl")
 typedef struct osl_table_info {
        struct osl_table_info *next;
        u32 instance;
-       char signature[ACPI_NAMESEG_SIZE];
+       char signature[ACPI_NAMESEG_SIZE] __nonstring;
 
 } osl_table_info;
 
tools/power/acpi/tools/acpidump/apfiles.c +1 -2
index 13817f9112c0..5a39b7d9351d 100644
@@ -103,7 +103,7 @@ int ap_open_output_file(char *pathname)
 
 int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance)
 {
-       char filename[ACPI_NAMESEG_SIZE + 16];
+       char filename[ACPI_NAMESEG_SIZE + 16] __nonstring;
        char instance_str[16];
        ACPI_FILE file;
        acpi_size actual;
--

2.47.2

#3 lore raw

Re: [RFC PATCH 1/2] ACPI: mark ACPI_COPY_NAMESEG destinations with __nonstring attribute

Quoted 11 lines · Rafael J. Wysocki
Quoted 9 lines · Ahmed Salem

strncpy(), which ACPI_COPY_NAMESEG currently uses, is deprecated[1].

This patch is the first of two, ultimately replacing strncpy() with strtomem(), avoiding future compiler warnings about truncation.

[1] https://github.com/KSPP/linux/issues/90

Quoted 9 lines

ACPICA material is primarily handled by the upstream ACPICA project on GitHub, so please avoid mixing ACPICA code changes with changes to the other code in one patch.

Also, ACPICA changes should first be submitted to upstream ACPICA, as indicated on this list for many times, see for instance:

https://lore.kernel.org/linux-acpi/CAJZ5v0gUDxrAn4W+Rf3ifjrg8Z9ZzTTOZjPFSSN5488mPqzXeA@mail.gmail.com/

Thank you so much for the note! In full transparency, I did see that comment prior to submitting this patch. However, I wasn't entirely sure whether I should have done that given the nature of this patch.

This marks my second time contributing patches as part of the LKMP, and I've just got started learning C fairly recently, so I thought I'd consolidate the changes into this series so if there's anything wrong with my approach, it would cut the time discussing it over on upstream as well as mailing lists. Hindsight being what it is, however, I should have seen the MR from Kees Cook introducing ACPI_NONSTRING before proposing changes, so that's definitely lessons learned for current and future reference.

Point taken nonetheless, and I will be submitting the ACPICA-related PR upstream, and resend the ACPI-specific one here instead without the RFC tag. My apologies, and thank you so much once again for your time and effort!

Quoted 101 lines
Quoted 101 lines
---
 drivers/acpi/acpica/acdebug.h                            | 2 +-
 drivers/acpi/prmt.c                                      | 2 +-
 drivers/acpi/sysfs.c                                     | 4 ++--
 include/acpi/actbl.h                                     | 6 +++---
 tools/power/acpi/os_specific/service_layers/oslinuxtbl.c | 2 +-
 tools/power/acpi/tools/acpidump/apfiles.c                | 2 +-
 6 files changed, 9 insertions(+), 9 deletions(-)
drivers/acpi/acpica/acdebug.h +1 -1
index 911875c5a5f1..2b56a8178f43 100644
@@ -37,7 +37,7 @@ struct acpi_db_argument_info {
 struct acpi_db_execute_walk {
        u32 count;
        u32 max_count;
-       char name_seg[ACPI_NAMESEG_SIZE + 1];
+       char name_seg[ACPI_NAMESEG_SIZE + 1] __nonstring;
 };
 
 #define PARAM_LIST(pl)                  pl
drivers/acpi/prmt.c +1 -1
index e549914a636c..ca70f01c940c 100644
@@ -40,7 +40,7 @@ struct prm_buffer {
 };
 
 struct prm_context_buffer {
-       char signature[ACPI_NAMESEG_SIZE];
+       char signature[ACPI_NAMESEG_SIZE] __nonstring;
        u16 revision;
        u16 reserved;
        guid_t identifier;
drivers/acpi/sysfs.c +2 -2
index a48ebbf768f9..a05d4032d4f1 100644
@@ -307,9 +307,9 @@ static struct kobject *hotplug_kobj;
 
 struct acpi_table_attr {
        struct bin_attribute attr;
-       char name[ACPI_NAMESEG_SIZE];
+       char name[ACPI_NAMESEG_SIZE] __nonstring;
        int instance;
-       char filename[ACPI_NAMESEG_SIZE+ACPI_INST_SIZE];
+       char filename[ACPI_NAMESEG_SIZE+ACPI_INST_SIZE] __nonstring;
        struct list_head node;
 };
 
include/acpi/actbl.h +3 -3
index 451f6276da49..8aa60281e7db 100644
@@ -66,12 +66,12 @@
  ******************************************************************************/
 
 struct acpi_table_header {
-       char signature[ACPI_NAMESEG_SIZE];      /* ASCII table signature */
+       char signature[ACPI_NAMESEG_SIZE] __nonstring;  /* ASCII table signature */
        u32 length;             /* Length of table in bytes, including this header */
        u8 revision;            /* ACPI Specification minor version number */
        u8 checksum;            /* To make sum of entire table == 0 */
-       char oem_id[ACPI_OEM_ID_SIZE];  /* ASCII OEM identification */
-       char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];      /* ASCII OEM table identification */
+       char oem_id[ACPI_OEM_ID_SIZE] __nonstring;      /* ASCII OEM identification */
+       char oem_table_id[ACPI_OEM_TABLE_ID_SIZE] __nonstring;  /* ASCII OEM table identification */
        u32 oem_revision;       /* OEM revision number */
        char asl_compiler_id[ACPI_NAMESEG_SIZE];        /* ASCII ASL compiler vendor ID */
        u32 asl_compiler_revision;      /* ASL compiler version */
tools/power/acpi/os_specific/service_layers/oslinuxtbl.c +1 -1
index 9d70d8c945af..52026b9e389e 100644
@@ -19,7 +19,7 @@ ACPI_MODULE_NAME("oslinuxtbl")
 typedef struct osl_table_info {
        struct osl_table_info *next;
        u32 instance;
-       char signature[ACPI_NAMESEG_SIZE];
+       char signature[ACPI_NAMESEG_SIZE] __nonstring;
 
 } osl_table_info;
 
tools/power/acpi/tools/acpidump/apfiles.c +1 -2
index 13817f9112c0..5a39b7d9351d 100644
@@ -103,7 +103,7 @@ int ap_open_output_file(char *pathname)
 
 int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance)
 {
-       char filename[ACPI_NAMESEG_SIZE + 16];
+       char filename[ACPI_NAMESEG_SIZE + 16] __nonstring;
        char instance_str[16];
        ACPI_FILE file;
        acpi_size actual;
--

2.47.2