Rename Strncat to StrnAppend, use it instead of strncat.

Strncat() looks almost like strncat(), but it's completely different. Change
the name to reduce confusion.

Also fix a place where strncat() was misused anyway.

BUG=none
BRANCH=all
TEST=manual

sudo FEATURES=test emerge vboot_reference
FEATURES=test emerge-$BOARD vboot_reference

Change-Id: I39c30d391aa6566ec67461462e800720ade66a91
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/44572
This commit is contained in:
Bill Richardson
2013-03-04 15:11:38 -08:00
committed by ChromeBot
parent 522da3e2c9
commit 5fed2a6670
6 changed files with 60 additions and 59 deletions

View File

@@ -331,6 +331,7 @@ TINYHOSTLIB_SRCS = \
utility/dump_kernel_config_lib.c \ utility/dump_kernel_config_lib.c \
firmware/lib/cgptlib/crc32.c \ firmware/lib/cgptlib/crc32.c \
firmware/lib/cgptlib/cgptlib_internal.c \ firmware/lib/cgptlib/cgptlib_internal.c \
firmware/lib/utility_string.c \
firmware/stub/utility_stub.c firmware/stub/utility_stub.c
TINYHOSTLIB_OBJS = ${TINYHOSTLIB_SRCS:%.c=${BUILD}/%.o} TINYHOSTLIB_OBJS = ${TINYHOSTLIB_SRCS:%.c=${BUILD}/%.o}

View File

@@ -8,7 +8,8 @@
#include "cgptlib_internal.h" #include "cgptlib_internal.h"
#include "cgpt_params.h" #include "cgpt_params.h"
#define _STUB_IMPLEMENTATION_
#include "utility.h"
static const char* DumpCgptAddParams(const CgptAddParams *params) { static const char* DumpCgptAddParams(const CgptAddParams *params) {
static char buf[256]; static char buf[256];
@@ -16,49 +17,49 @@ static const char* DumpCgptAddParams(const CgptAddParams *params) {
buf[0] = 0; buf[0] = 0;
snprintf(tmp, sizeof(tmp), "-i %d ", params->partition); snprintf(tmp, sizeof(tmp), "-i %d ", params->partition);
strncat(buf, tmp, sizeof(buf)); StrnAppend(buf, tmp, sizeof(buf));
if (params->label) { if (params->label) {
snprintf(tmp, sizeof(tmp), "-l %s ", params->label); snprintf(tmp, sizeof(tmp), "-l %s ", params->label);
strncat(buf, tmp, sizeof(buf)); StrnAppend(buf, tmp, sizeof(buf));
} }
if (params->set_begin) { if (params->set_begin) {
snprintf(tmp, sizeof(tmp), "-b %llu ", (unsigned long long)params->begin); snprintf(tmp, sizeof(tmp), "-b %llu ", (unsigned long long)params->begin);
strncat(buf, tmp, sizeof(buf)); StrnAppend(buf, tmp, sizeof(buf));
} }
if (params->set_size) { if (params->set_size) {
snprintf(tmp, sizeof(tmp), "-s %llu ", (unsigned long long)params->size); snprintf(tmp, sizeof(tmp), "-s %llu ", (unsigned long long)params->size);
strncat(buf, tmp, sizeof(buf)); StrnAppend(buf, tmp, sizeof(buf));
} }
if (params->set_type) { if (params->set_type) {
GuidToStr(&params->type_guid, tmp, sizeof(tmp)); GuidToStr(&params->type_guid, tmp, sizeof(tmp));
strncat(buf, "-t ", sizeof(buf)); StrnAppend(buf, "-t ", sizeof(buf));
strncat(buf, tmp, sizeof(buf)); StrnAppend(buf, tmp, sizeof(buf));
strncat(buf, " ", sizeof(buf)); StrnAppend(buf, " ", sizeof(buf));
} }
if (params->set_unique) { if (params->set_unique) {
GuidToStr(&params->unique_guid, tmp, sizeof(tmp)); GuidToStr(&params->unique_guid, tmp, sizeof(tmp));
strncat(buf, "-u ", sizeof(buf)); StrnAppend(buf, "-u ", sizeof(buf));
strncat(buf, tmp, sizeof(buf)); StrnAppend(buf, tmp, sizeof(buf));
strncat(buf, " ", sizeof(buf)); StrnAppend(buf, " ", sizeof(buf));
} }
if (params->set_successful) { if (params->set_successful) {
snprintf(tmp, sizeof(tmp), "-S %d ", params->successful); snprintf(tmp, sizeof(tmp), "-S %d ", params->successful);
strncat(buf, tmp, sizeof(buf)); StrnAppend(buf, tmp, sizeof(buf));
} }
if (params->set_tries) { if (params->set_tries) {
snprintf(tmp, sizeof(tmp), "-T %d ", params->tries); snprintf(tmp, sizeof(tmp), "-T %d ", params->tries);
strncat(buf, tmp, sizeof(buf)); StrnAppend(buf, tmp, sizeof(buf));
} }
if (params->set_priority) { if (params->set_priority) {
snprintf(tmp, sizeof(tmp), "-P %d ", params->priority); snprintf(tmp, sizeof(tmp), "-P %d ", params->priority);
strncat(buf, tmp, sizeof(buf)); StrnAppend(buf, tmp, sizeof(buf));
} }
if (params->set_raw) { if (params->set_raw) {
snprintf(tmp, sizeof(tmp), "-A 0x%x ", params->raw_value); snprintf(tmp, sizeof(tmp), "-A 0x%x ", params->raw_value);
strncat(buf, tmp, sizeof(buf)); StrnAppend(buf, tmp, sizeof(buf));
} }
strncat(buf, "\n", sizeof(buf)); StrnAppend(buf, "\n", sizeof(buf));
return buf; return buf;
} }

View File

@@ -118,7 +118,7 @@ uint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value,
* null-terminated if <destlen> > 0. Returns the number of characters used in * null-terminated if <destlen> > 0. Returns the number of characters used in
* <dest>, not counting the terminating null. * <dest>, not counting the terminating null.
*/ */
uint32_t Strncat(char *dest, const char *src, uint32_t destlen); uint32_t StrnAppend(char *dest, const char *src, uint32_t destlen);
/* Ensure that only our stub implementations are used, not standard C */ /* Ensure that only our stub implementations are used, not standard C */
#ifndef _STUB_IMPLEMENTATION_ #ifndef _STUB_IMPLEMENTATION_

View File

@@ -52,11 +52,11 @@ uint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value,
return usedsize - 1; return usedsize - 1;
} }
uint32_t Strncat(char *dest, const char *src, uint32_t destlen) uint32_t StrnAppend(char *dest, const char *src, uint32_t destlen)
{ {
uint32_t used = 0; uint32_t used = 0;
if (!dest || !src) if (!dest || !src || !destlen)
return 0; return 0;
/* Skip past existing string in destination.*/ /* Skip past existing string in destination.*/

View File

@@ -66,7 +66,6 @@ const char *VbHWID(VbCommonParams *cparams)
* in-memory font structure distinct from the in-flash version. We'll do that * in-memory font structure distinct from the in-flash version. We'll do that
* Real Soon Now. Until then, we just repeat the same linear search every time. * Real Soon Now. Until then, we just repeat the same linear search every time.
*/ */
typedef FontArrayHeader VbFont_t;
VbFont_t *VbInternalizeFontData(FontArrayHeader *fonthdr) VbFont_t *VbInternalizeFontData(FontArrayHeader *fonthdr)
{ {
@@ -366,11 +365,11 @@ VbError_t VbDisplayScreenFromGBB(VbCommonParams *cparams, uint32_t screen,
*/ */
if (gbb->major_version == GBB_MAJOR_VER && gbb->minor_version >= 1 && if (gbb->major_version == GBB_MAJOR_VER && gbb->minor_version >= 1 &&
(gbb->flags != 0)) { (gbb->flags != 0)) {
used += Strncat(outbuf + used, "gbb.flags is nonzero: 0x", used += StrnAppend(outbuf + used, "gbb.flags is nonzero: 0x",
OUTBUF_LEN - used); OUTBUF_LEN - used);
used += Uint64ToString(outbuf + used, OUTBUF_LEN - used, used += Uint64ToString(outbuf + used, OUTBUF_LEN - used,
gbb->flags, 16, 8); gbb->flags, 16, 8);
used += Strncat(outbuf + used, "\n", OUTBUF_LEN - used); used += StrnAppend(outbuf + used, "\n", OUTBUF_LEN - used);
(void)VbExDisplayDebugInfo(outbuf); (void)VbExDisplayDebugInfo(outbuf);
} }
@@ -581,98 +580,98 @@ VbError_t VbDisplayDebugInfo(VbCommonParams *cparams, VbNvContext *vncptr)
VbDisplayScreen(cparams, disp_current_screen, 1, vncptr); VbDisplayScreen(cparams, disp_current_screen, 1, vncptr);
/* Add hardware ID */ /* Add hardware ID */
used += Strncat(buf + used, "HWID: ", DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, "HWID: ", DEBUG_INFO_SIZE - used);
if (0 == gbb->hwid_size || if (0 == gbb->hwid_size ||
gbb->hwid_offset > cparams->gbb_size || gbb->hwid_offset > cparams->gbb_size ||
gbb->hwid_offset + gbb->hwid_size > cparams->gbb_size) { gbb->hwid_offset + gbb->hwid_size > cparams->gbb_size) {
VBDEBUG(("VbDisplayDebugInfo(): invalid hwid offset/size\n")); VBDEBUG(("VbDisplayDebugInfo(): invalid hwid offset/size\n"));
used += Strncat(buf + used, used += StrnAppend(buf + used,
"(INVALID)", DEBUG_INFO_SIZE - used); "(INVALID)", DEBUG_INFO_SIZE - used);
} else { } else {
used += Strncat(buf + used, used += StrnAppend(buf + used,
(char *)((uint8_t *)gbb + gbb->hwid_offset), (char *)((uint8_t *)gbb + gbb->hwid_offset),
DEBUG_INFO_SIZE - used); DEBUG_INFO_SIZE - used);
} }
/* Add recovery reason */ /* Add recovery reason */
used += Strncat(buf + used, used += StrnAppend(buf + used,
"\nrecovery_reason: 0x", DEBUG_INFO_SIZE - used); "\nrecovery_reason: 0x", DEBUG_INFO_SIZE - used);
used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
shared->recovery_reason, 16, 2); shared->recovery_reason, 16, 2);
used += Strncat(buf + used, " ", DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, " ", DEBUG_INFO_SIZE - used);
used += Strncat(buf + used, used += StrnAppend(buf + used,
RecoveryReasonString(shared->recovery_reason), RecoveryReasonString(shared->recovery_reason),
DEBUG_INFO_SIZE - used); DEBUG_INFO_SIZE - used);
/* Add VbSharedData flags */ /* Add VbSharedData flags */
used += Strncat(buf + used, "\nVbSD.flags: 0x", DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, "\nVbSD.flags: 0x", DEBUG_INFO_SIZE - used);
used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
shared->flags, 16, 8); shared->flags, 16, 8);
/* Add raw contents of VbNvStorage */ /* Add raw contents of VbNvStorage */
used += Strncat(buf + used, "\nVbNv.raw:", DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, "\nVbNv.raw:", DEBUG_INFO_SIZE - used);
for (i = 0; i < VBNV_BLOCK_SIZE; i++) { for (i = 0; i < VBNV_BLOCK_SIZE; i++) {
used += Strncat(buf + used, " ", DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, " ", DEBUG_INFO_SIZE - used);
used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
vncptr->raw[i], 16, 2); vncptr->raw[i], 16, 2);
} }
/* Add dev_boot_usb flag */ /* Add dev_boot_usb flag */
VbNvGet(vncptr, VBNV_DEV_BOOT_USB, &i); VbNvGet(vncptr, VBNV_DEV_BOOT_USB, &i);
used += Strncat(buf + used, "\ndev_boot_usb: ", DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, "\ndev_boot_usb: ", DEBUG_INFO_SIZE - used);
used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0); used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0);
/* Add dev_boot_legacy flag */ /* Add dev_boot_legacy flag */
VbNvGet(vncptr, VBNV_DEV_BOOT_LEGACY, &i); VbNvGet(vncptr, VBNV_DEV_BOOT_LEGACY, &i);
used += Strncat(buf + used, used += StrnAppend(buf + used,
"\ndev_boot_legacy: ", DEBUG_INFO_SIZE - used); "\ndev_boot_legacy: ", DEBUG_INFO_SIZE - used);
used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0); used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0);
/* Add dev_boot_signed_only flag */ /* Add dev_boot_signed_only flag */
VbNvGet(vncptr, VBNV_DEV_BOOT_SIGNED_ONLY, &i); VbNvGet(vncptr, VBNV_DEV_BOOT_SIGNED_ONLY, &i);
used += Strncat(buf + used, "\ndev_boot_signed_only: ", used += StrnAppend(buf + used, "\ndev_boot_signed_only: ",
DEBUG_INFO_SIZE - used); DEBUG_INFO_SIZE - used);
used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0); used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0);
/* Add TPM versions */ /* Add TPM versions */
used += Strncat(buf + used, "\nTPM: fwver=0x", DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, "\nTPM: fwver=0x", DEBUG_INFO_SIZE - used);
used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
shared->fw_version_tpm, 16, 8); shared->fw_version_tpm, 16, 8);
used += Strncat(buf + used, " kernver=0x", DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, " kernver=0x", DEBUG_INFO_SIZE - used);
used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
shared->kernel_version_tpm, 16, 8); shared->kernel_version_tpm, 16, 8);
/* Add GBB flags */ /* Add GBB flags */
used += Strncat(buf + used, "\ngbb.flags: 0x", DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, "\ngbb.flags: 0x", DEBUG_INFO_SIZE - used);
if (gbb->major_version == GBB_MAJOR_VER && gbb->minor_version >= 1) { if (gbb->major_version == GBB_MAJOR_VER && gbb->minor_version >= 1) {
used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
gbb->flags, 16, 8); gbb->flags, 16, 8);
} else { } else {
used += Strncat(buf + used, used += StrnAppend(buf + used,
"0 (default)", DEBUG_INFO_SIZE - used); "0 (default)", DEBUG_INFO_SIZE - used);
} }
/* Add sha1sum for Root & Recovery keys */ /* Add sha1sum for Root & Recovery keys */
FillInSha1Sum(sha1sum, FillInSha1Sum(sha1sum,
(VbPublicKey *)((uint8_t *)gbb + gbb->rootkey_offset)); (VbPublicKey *)((uint8_t *)gbb + gbb->rootkey_offset));
used += Strncat(buf + used, "\ngbb.rootkey: ", DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, "\ngbb.rootkey: ", DEBUG_INFO_SIZE - used);
used += Strncat(buf + used, sha1sum, DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, sha1sum, DEBUG_INFO_SIZE - used);
FillInSha1Sum(sha1sum, FillInSha1Sum(sha1sum,
(VbPublicKey *)((uint8_t *)gbb + gbb->recovery_key_offset)); (VbPublicKey *)((uint8_t *)gbb + gbb->recovery_key_offset));
used += Strncat(buf + used, used += StrnAppend(buf + used,
"\ngbb.recovery_key: ", DEBUG_INFO_SIZE - used); "\ngbb.recovery_key: ", DEBUG_INFO_SIZE - used);
used += Strncat(buf + used, sha1sum, DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, sha1sum, DEBUG_INFO_SIZE - used);
/* If we're in dev-mode, show the kernel subkey that we expect, too. */ /* If we're in dev-mode, show the kernel subkey that we expect, too. */
if (0 == shared->recovery_reason) { if (0 == shared->recovery_reason) {
FillInSha1Sum(sha1sum, &shared->kernel_subkey); FillInSha1Sum(sha1sum, &shared->kernel_subkey);
used += Strncat(buf + used, used += StrnAppend(buf + used,
"\nkernel_subkey: ", DEBUG_INFO_SIZE - used); "\nkernel_subkey: ", DEBUG_INFO_SIZE - used);
used += Strncat(buf + used, sha1sum, DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, sha1sum, DEBUG_INFO_SIZE - used);
} }
/* Make sure we finish with a newline */ /* Make sure we finish with a newline */
used += Strncat(buf + used, "\n", DEBUG_INFO_SIZE - used); used += StrnAppend(buf + used, "\n", DEBUG_INFO_SIZE - used);
/* TODO: add more interesting data: /* TODO: add more interesting data:
* - Information on current disks */ * - Information on current disks */

View File

@@ -19,38 +19,38 @@ static void StrncatTest(void) {
char dest[128]; char dest[128];
/* Null inputs */ /* Null inputs */
TEST_EQ(0, Strncat(dest, NULL, sizeof(dest)), "Strncat('', null)"); TEST_EQ(0, StrnAppend(dest, NULL, sizeof(dest)), "StrnAppend('', null)");
TEST_EQ(0, Strncat(NULL, "Hey!", sizeof(dest)), "Strncat(null, '')"); TEST_EQ(0, StrnAppend(NULL, "Hey!", sizeof(dest)), "StrnAppend(null, '')");
/* Empty <-- empty */ /* Empty <-- empty */
*dest = 0; *dest = 0;
TEST_EQ(0, Strncat(dest, "", sizeof(dest)), "Strncat('', '')"); TEST_EQ(0, StrnAppend(dest, "", sizeof(dest)), "StrnAppend('', '')");
TEST_EQ(0, strcmp(dest, ""), "Strncat('', '') result"); TEST_EQ(0, strcmp(dest, ""), "StrnAppend('', '') result");
/* Nonempty <-- empty */ /* Nonempty <-- empty */
strcpy(dest, "Bob"); strcpy(dest, "Bob");
TEST_EQ(3, Strncat(dest, "", sizeof(dest)), "Strncat(B, '')"); TEST_EQ(3, StrnAppend(dest, "", sizeof(dest)), "StrnAppend(B, '')");
TEST_EQ(0, strcmp(dest, "Bob"), "Strncat(B, '') result"); TEST_EQ(0, strcmp(dest, "Bob"), "StrnAppend(B, '') result");
/* Empty <-- nonempty */ /* Empty <-- nonempty */
*dest = 0; *dest = 0;
TEST_EQ(5, Strncat(dest, "Alice", sizeof(dest)), "Strncat('', A)"); TEST_EQ(5, StrnAppend(dest, "Alice", sizeof(dest)), "StrnAppend('', A)");
TEST_EQ(0, strcmp(dest, "Alice"), "Strncat('', A) result"); TEST_EQ(0, strcmp(dest, "Alice"), "StrnAppend('', A) result");
/* Nonempty <-- nonempty */ /* Nonempty <-- nonempty */
strcpy(dest, "Tigre"); strcpy(dest, "Tigre");
TEST_EQ(10, Strncat(dest, "Bunny", sizeof(dest)), "Strncat(T, B)"); TEST_EQ(10, StrnAppend(dest, "Bunny", sizeof(dest)), "StrnAppend(T, B)");
TEST_EQ(0, strcmp(dest, "TigreBunny"), "Strncat(T, B) result"); TEST_EQ(0, strcmp(dest, "TigreBunny"), "StrnAppend(T, B) result");
/* Test clipping */ /* Test clipping */
strcpy(dest, "YesI"); strcpy(dest, "YesI");
TEST_EQ(7, Strncat(dest, "Can't", 8), "Strncat(Y, over)"); TEST_EQ(7, StrnAppend(dest, "Can't", 8), "StrnAppend(Y, over)");
TEST_EQ(0, strcmp(dest, "YesICan"), "Strncat(Y, over) result"); TEST_EQ(0, strcmp(dest, "YesICan"), "StrnAppend(Y, over) result");
/* Test clipping if dest already overflows its claimed length */ /* Test clipping if dest already overflows its claimed length */
strcpy(dest, "BudgetDeficit"); strcpy(dest, "BudgetDeficit");
TEST_EQ(6, Strncat(dest, "Spending", 7), "Strncat(over, over)"); TEST_EQ(6, StrnAppend(dest, "Spending", 7), "StrnAppend(over, over)");
TEST_EQ(0, strcmp(dest, "Budget"), "Strncat(over, over) result"); TEST_EQ(0, strcmp(dest, "Budget"), "StrnAppend(over, over) result");
} }