diff options
author | Thomas Huth <thuth@redhat.com> | 2015-11-25 20:58:18 +0100 |
---|---|---|
committer | Alexey Kardashevskiy <aik@ozlabs.ru> | 2015-12-01 17:02:04 +1100 |
commit | f0d251a0775572ebce8566c16e4482455b9efd84 (patch) | |
tree | 8a54444507eeb17d4c0a351f87378db8e777b76e /lib/libnvram | |
parent | edd582383f9800e61b48669f7515e5d334ef961b (diff) | |
download | SLOF-f0d251a0775572ebce8566c16e4482455b9efd84.zip SLOF-f0d251a0775572ebce8566c16e4482455b9efd84.tar.gz SLOF-f0d251a0775572ebce8566c16e4482455b9efd84.tar.bz2 |
Rework wrapper for new_nvram_partition() and fix possible bug in there
The wrapper for new_nvram_partition() is using a 12 bytes buffer to
create a zero-terminated string. However, if the string has exactly
12 characters, the final NUL-terminator is missing. new_nvram_partition()
then calls create_nvram_partition() internally which depends on proper
NUL-terminated strings. So fix this by making sure that the copied
string is always NUL-terminated - and while we're at it, also move
the copy code out of libnvram.code to save some precious bytes in the
stack space of the engine() function.
Signed-off-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Diffstat (limited to 'lib/libnvram')
-rw-r--r-- | lib/libnvram/libnvram.code | 17 | ||||
-rw-r--r-- | lib/libnvram/nvram.c | 16 | ||||
-rw-r--r-- | lib/libnvram/nvram.h | 1 |
3 files changed, 22 insertions, 12 deletions
diff --git a/lib/libnvram/libnvram.code b/lib/libnvram/libnvram.code index 723941d..427adc2 100644 --- a/lib/libnvram/libnvram.code +++ b/lib/libnvram/libnvram.code @@ -103,23 +103,16 @@ MIRP /* new-nvram-partition ( type name.addr name.len len -- part.offs part.len FALSE | TRUE) */ PRIM(new_X2d_nvram_X2d_partition) - int type, len, i, slen; - char name[12], *addr; + int type, len, namelen; partition_t partition; + char *name; len = TOS.u; POP; - slen = TOS.u; POP; - addr = (char *)TOS.u; POP; + namelen = TOS.u; POP; + name = (char *)TOS.u; POP; type = TOS.u; POP; - for (i=0; i<12; i++) { - if(slen>i) - name[i]=addr[i]; - else - name[i]=0; - } - - partition=new_nvram_partition(type, name, len); + partition = new_nvram_partition_fs(type, name, namelen, len); if(!partition.len) { PUSH; TOS.u = -1; // TRUE diff --git a/lib/libnvram/nvram.c b/lib/libnvram/nvram.c index 5c11376..1a4f91a 100644 --- a/lib/libnvram/nvram.c +++ b/lib/libnvram/nvram.c @@ -466,6 +466,22 @@ partition_t new_nvram_partition(int type, char *name, int len) return new_part; } +partition_t new_nvram_partition_fs(int type, char *name, int namelen, int len) +{ + char buf[13]; + int i; + + for (i = 0; i < 12; i++) { + if (i < namelen) + buf[i] = name[i]; + else + buf[i] = 0; + } + buf[12] = 0; + + return new_nvram_partition(type, buf, len); +} + /** * @param partition partition structure pointing to the partition to wipe. */ diff --git a/lib/libnvram/nvram.h b/lib/libnvram/nvram.h index fa6bdd4..b4964f6 100644 --- a/lib/libnvram/nvram.h +++ b/lib/libnvram/nvram.h @@ -54,6 +54,7 @@ partition_t get_partition(unsigned int type, char *name); void erase_nvram(int offset, int len); int wipe_partition(partition_t partition, int header_only); partition_t new_nvram_partition(int type, char *name, int len); +partition_t new_nvram_partition_fs(int type, char *name, int namelen, int len); int increase_nvram_partition_size(partition_t partition, int newsize); int clear_nvram_partition(partition_t part); int delete_nvram_partition(partition_t part); |