Adjust strncpy sizes
When building under bitbake with the latest openbmc, we get compile
warnings such as these:
```
| ../git/sections/cper-section-nvidia.c: In function 'ir_section_nvidia_to_cper':
| ../git/sections/cper-section-nvidia.c:67:9: error: '__builtin_strncpy' specified bound 16 equals destination size [-Werror=stringop-truncation]
| 67 | strncpy(section_cper->Signature,
```
Using `strncpy` on its own is unsafe because a string too long will
end up in the destination buffer without NUL termination. Adjust
the strncpy to be one shorter than the buffer and force the trailing
byte to be a NUL.
Repeat this pattern for all `strncpy` calls.
Change-Id: I45c630733f0138d2b089a60f698d75e1c09de9e2
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/generator/sections/gen-section-nvidia.c b/generator/sections/gen-section-nvidia.c
index 83ed84e..d5103a6 100644
--- a/generator/sections/gen-section-nvidia.c
+++ b/generator/sections/gen-section-nvidia.c
@@ -36,7 +36,8 @@
//Signature.
int idx_random = rand() % (sizeof(signatures) / sizeof(signatures[0]));
strncpy(nvidia_error->Signature, signatures[idx_random],
- sizeof(nvidia_error->Signature));
+ sizeof(nvidia_error->Signature) - 1);
+ nvidia_error->Signature[sizeof(nvidia_error->Signature) - 1] = '\0';
//Set return values, exit.
*location = section;
diff --git a/ir-parse.c b/ir-parse.c
index 0287337..517de45 100644
--- a/ir-parse.c
+++ b/ir-parse.c
@@ -240,7 +240,8 @@
json_object_object_get(section_descriptor_ir, "fruText");
if (fru_text != NULL) {
strncpy(descriptor->FruString, json_object_get_string(fru_text),
- 20);
+ sizeof(descriptor->FruString) - 1);
+ descriptor->FruString[sizeof(descriptor->FruString) - 1] = '\0';
}
}
diff --git a/sections/cper-section-generic.c b/sections/cper-section-generic.c
index 05f790f..8882b78 100644
--- a/sections/cper-section-generic.c
+++ b/sections/cper-section-generic.c
@@ -143,7 +143,11 @@
const char *brand_string = json_object_get_string(
json_object_object_get(section, "cpuBrandString"));
if (brand_string != NULL) {
- strncpy(section_cper->BrandString, brand_string, 127);
+ strncpy(section_cper->BrandString, brand_string,
+ sizeof(section_cper->BrandString) - 1);
+ section_cper
+ ->BrandString[sizeof(section_cper->BrandString) - 1] =
+ '\0';
}
//Write & flush out to file, free memory.
diff --git a/sections/cper-section-nvidia.c b/sections/cper-section-nvidia.c
index f0ccec6..e2f8aef 100644
--- a/sections/cper-section-nvidia.c
+++ b/sections/cper-section-nvidia.c
@@ -67,7 +67,8 @@
strncpy(section_cper->Signature,
json_object_get_string(
json_object_object_get(section, "signature")),
- sizeof(section_cper->Signature));
+ sizeof(section_cper->Signature) - 1);
+ section_cper->Signature[sizeof(section_cper->Signature) - 1] = '\0';
//Fields.
section_cper->ErrorType = json_object_get_int(