Make crossystem.h more polite and more useful.

This adds a VB_MAX_STRING_PROPERTY for callers that don't
want to guess at how big to make their buffers.

Additionally, it changes the size parameter to VbGetPropertyString()
from int to size_t.

BUG=None
TEST=compile the code
BRANCH=none

Change-Id: I22809d48e13b535593cb22a56444e2dcb27791a5
Reviewed-on: https://chromium-review.googlesource.com/175039
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Tested-by: Richard Barnette <jrbarnette@chromium.org>
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Commit-Queue: Richard Barnette <jrbarnette@chromium.org>
This commit is contained in:
J. Richard Barnette
2013-10-30 11:36:45 -07:00
committed by chrome-internal-fetch
parent fe2c1a231e
commit a3d70a3d2b
6 changed files with 30 additions and 12 deletions

View File

@@ -5,6 +5,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <sys/types.h> #include <sys/types.h>
@@ -533,7 +534,8 @@ int VbGetArchPropertyInt(const char* name) {
return -1; return -1;
} }
const char* VbGetArchPropertyString(const char* name, char* dest, int size) { const char* VbGetArchPropertyString(const char* name, char* dest,
size_t size) {
char *str = NULL; char *str = NULL;
char *rv = NULL; char *rv = NULL;
char *prop = NULL; char *prop = NULL;

View File

@@ -7,6 +7,7 @@
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <linux/nvram.h> #include <linux/nvram.h>
#include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -671,7 +672,8 @@ int VbGetArchPropertyInt(const char* name) {
} }
const char* VbGetArchPropertyString(const char* name, char* dest, int size) { const char* VbGetArchPropertyString(const char* name, char* dest,
size_t size) {
if (!strcasecmp(name,"arch")) { if (!strcasecmp(name,"arch")) {
return StrCopy(dest, "x86", size); return StrCopy(dest, "x86", size);

View File

@@ -10,6 +10,12 @@
extern "C" { extern "C" {
#endif #endif
#include <stddef.h>
/* Recommended size for string property buffers used with
* VbGetSystemPropertyString(). */
#define VB_MAX_STRING_PROPERTY ((size_t) 8192)
/* Reads a system property integer. /* Reads a system property integer.
* *
* Returns the property value, or -1 if error. */ * Returns the property value, or -1 if error. */
@@ -19,8 +25,12 @@ int VbGetSystemPropertyInt(const char* name);
* specified size. Returned string will be null-terminated. If the * specified size. Returned string will be null-terminated. If the
* buffer is too small, the returned string will be truncated. * buffer is too small, the returned string will be truncated.
* *
* The caller can expect an un-truncated value if the size provided is
* at least VB_MAX_STRING_PROPERTY.
*
* Returns the passed buffer, or NULL if error. */ * Returns the passed buffer, or NULL if error. */
const char* VbGetSystemPropertyString(const char* name, char* dest, int size); const char* VbGetSystemPropertyString(const char* name, char* dest,
size_t size);
/* Sets a system property integer. /* Sets a system property integer.
* *
@@ -28,6 +38,9 @@ const char* VbGetSystemPropertyString(const char* name, char* dest, int size);
int VbSetSystemPropertyInt(const char* name, int value); int VbSetSystemPropertyInt(const char* name, int value);
/* Set a system property string. /* Set a system property string.
*
* The maximum length of the value accepted depends on the specific
* property, not on VB_MAX_STRING_PROPERTY.
* *
* Returns 0 if success, -1 if error. */ * Returns 0 if success, -1 if error. */
int VbSetSystemPropertyString(const char* name, const char* value); int VbSetSystemPropertyString(const char* name, const char* value);

View File

@@ -3,6 +3,7 @@
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
@@ -69,7 +70,7 @@ typedef enum VbBuildOption {
/* Return true if the FWID starts with the specified string. */ /* Return true if the FWID starts with the specified string. */
int FwidStartsWith(const char *start) { int FwidStartsWith(const char *start) {
char fwid[128]; char fwid[VB_MAX_STRING_PROPERTY];
if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid))) if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid)))
return 0; return 0;
@@ -492,7 +493,8 @@ int VbGetSystemPropertyInt(const char* name) {
} }
const char* VbGetSystemPropertyString(const char* name, char* dest, int size) { const char* VbGetSystemPropertyString(const char* name, char* dest,
size_t size) {
static const char unknown_string[] = "unknown"; static const char unknown_string[] = "unknown";
/* Check architecture-dependent properties first */ /* Check architecture-dependent properties first */

View File

@@ -8,6 +8,8 @@
#ifndef VBOOT_REFERENCE_CROSSYSTEM_ARCH_H_ #ifndef VBOOT_REFERENCE_CROSSYSTEM_ARCH_H_
#define VBOOT_REFERENCE_CROSSYSTEM_ARCH_H_ #define VBOOT_REFERENCE_CROSSYSTEM_ARCH_H_
#include <stddef.h>
#include "vboot_nvstorage.h" #include "vboot_nvstorage.h"
#include "vboot_struct.h" #include "vboot_struct.h"
@@ -76,7 +78,7 @@ int VbGetArchPropertyInt(const char* name);
* will be truncated. * will be truncated.
* *
* Returns the passed buffer, or NULL if error. */ * Returns the passed buffer, or NULL if error. */
const char* VbGetArchPropertyString(const char* name, char* dest, int size); const char* VbGetArchPropertyString(const char* name, char* dest, size_t size);
/* Set an architecture-specific system property integer. /* Set an architecture-specific system property integer.
* *

View File

@@ -11,9 +11,6 @@
#include "crossystem.h" #include "crossystem.h"
/* Max length of a string parameter */
#define MAX_STRING 8192
/* /*
* Call arch specific init, if provided, otherwise use the 'weak' stub. * Call arch specific init, if provided, otherwise use the 'weak' stub.
*/ */
@@ -153,7 +150,7 @@ int SetParam(const Param* p, const char* value) {
* Returns 0 if success (match), non-zero if error (mismatch). */ * Returns 0 if success (match), non-zero if error (mismatch). */
int CheckParam(const Param* p, char* expect) { int CheckParam(const Param* p, char* expect) {
if (p->flags & IS_STRING) { if (p->flags & IS_STRING) {
char buf[MAX_STRING]; char buf[VB_MAX_STRING_PROPERTY];
const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
if (!v || 0 != strcmp(v, expect)) if (!v || 0 != strcmp(v, expect))
return 1; return 1;
@@ -175,7 +172,7 @@ int CheckParam(const Param* p, char* expect) {
* Returns 0 if success, non-zero if error. */ * Returns 0 if success, non-zero if error. */
int PrintParam(const Param* p) { int PrintParam(const Param* p) {
if (p->flags & IS_STRING) { if (p->flags & IS_STRING) {
char buf[MAX_STRING]; char buf[VB_MAX_STRING_PROPERTY];
const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf));
if (!v) if (!v)
return 1; return 1;
@@ -197,7 +194,7 @@ int PrintParam(const Param* p) {
int PrintAllParams(int force_all) { int PrintAllParams(int force_all) {
const Param* p; const Param* p;
int retval = 0; int retval = 0;
char buf[MAX_STRING]; char buf[VB_MAX_STRING_PROPERTY];
const char* value; const char* value;
for (p = sys_param_list; p->name; p++) { for (p = sys_param_list; p->name; p++) {