Skip to content

Commit 1747d31

Browse files
committed
Drop memory_read_str function
The memory_read_str function tends to terminate the string with '\0' character but the string is only terminated with '\0' character if src is less than max characters long according to "man 3 strncpy". As a result, there is a chance to make the dst is not terminated with '\0' character, e.g., the length of filename equals 256 characters long. It can simply solved by passing "sizeof(name_str) - 1" to memory_read_str function instead of "sizeof(name_str)" when calling memory_read_str function. But, to deal with the filename >= 256 characters long, malloc the desired length of filename during runtime is a better way.
1 parent ccf8b9b commit 1747d31

File tree

3 files changed

+9
-24
lines changed

3 files changed

+9
-24
lines changed

src/io.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,6 @@ void memory_read(const memory_t *mem,
6161
memcpy(dst, mem->mem_base + addr, size);
6262
}
6363

64-
uint32_t memory_read_str(const memory_t *mem,
65-
uint8_t *dst,
66-
uint32_t addr,
67-
uint32_t max)
68-
{
69-
char *d = (char *) dst;
70-
char *s = (char *) mem->mem_base + addr;
71-
return strlen(strncpy(d, s, max));
72-
}
73-
7464
uint32_t memory_ifetch(uint32_t addr)
7565
{
7666
return *(const uint32_t *) (data_memory_base + addr);

src/io.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ typedef struct {
2020
memory_t *memory_new(uint32_t size);
2121
void memory_delete(memory_t *m);
2222

23-
/* read a C-style string from memory */
24-
uint32_t memory_read_str(const memory_t *m,
25-
uint8_t *dst,
26-
uint32_t addr,
27-
uint32_t max);
28-
2923
/* read an instruction from memory */
3024
uint32_t memory_ifetch(uint32_t addr);
3125

src/syscall.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdint.h>
88
#include <stdio.h>
99
#include <stdlib.h>
10+
#include <string.h>
1011
#include <sys/time.h>
1112

1213
#include "riscv.h"
@@ -84,7 +85,7 @@ static void syscall_write(riscv_t *rv)
8485
{
8586
vm_attr_t *attr = PRIV(rv);
8687

87-
/* _write(fde, buffer, count) */
88+
/* _write(fd, buffer, count) */
8889
riscv_word_t fd = rv_get_reg(rv, rv_reg_a0);
8990
riscv_word_t buffer = rv_get_reg(rv, rv_reg_a1);
9091
riscv_word_t count = rv_get_reg(rv, rv_reg_a2);
@@ -327,13 +328,11 @@ static void syscall_open(riscv_t *rv)
327328
uint32_t mode = rv_get_reg(rv, rv_reg_a2);
328329

329330
/* read name from runtime memory */
330-
char name_str[256] = {'\0'};
331-
uint32_t read = memory_read_str(attr->mem, (uint8_t *) name_str, name,
332-
sizeof(name_str));
333-
if (read > sizeof(name_str)) {
334-
rv_set_reg(rv, rv_reg_a0, -1);
335-
return;
336-
}
331+
const size_t name_len = strlen(attr->mem->mem_base + name);
332+
char *name_str = malloc(name_len + 1);
333+
assert(name_str);
334+
name_str[name_len] = '\0';
335+
memory_read(attr->mem, (uint8_t *) name_str, name, name_len);
337336

338337
/* open the file */
339338
const char *mode_str = get_mode_str(flags, mode);
@@ -348,6 +347,8 @@ static void syscall_open(riscv_t *rv)
348347
return;
349348
}
350349

350+
free(name_str);
351+
351352
const int fd = find_free_fd(attr); /* find a free file descriptor */
352353

353354
/* insert into the file descriptor map */

0 commit comments

Comments
 (0)