Skip to content

Commit e418d8d

Browse files
committed
tutorials: root_of_trust: reduce redundant code using strlcpy in remaining apps
1 parent 5ad5d21 commit e418d8d

File tree

4 files changed

+12
-24
lines changed
  • examples/tutorials/root_of_trust
    • encryption_service_milestone_one
    • encryption_service_milestone_three
    • encryption_service_milestone_two
    • suspicious_service_milestone_one

4 files changed

+12
-24
lines changed

examples/tutorials/root_of_trust/encryption_service_milestone_one/main.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "libtock/tock.h"
88
#include <stdio.h>
99
#include <stdlib.h>
10+
#include <string.h>
1011

1112
#include <libtock-sync/interface/console.h>
1213
#include <libtock/kernel/ipc.h>
@@ -63,12 +64,8 @@ static int setup_logging() {
6364
static int log_to_screen(const char* message) {
6465
returncode_t ret;
6566

66-
// Copy up to the log buffer's size of the message, with room for a null byte.
67-
uint16_t len = strnlen(message, sizeof(log_buf) - 1);
68-
memcpy(log_buf, message, len);
69-
70-
// Add the null byte.
71-
log_buf[len] = '\0';
67+
// Load the log buffer with our message
68+
strlcpy(log_buf, message, LOG_WIDTH);
7269

7370
// Start the logging process.
7471
ret = ipc_notify_service(screen_service);

examples/tutorials/root_of_trust/encryption_service_milestone_three/main.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "libtock/tock.h"
88
#include <stdio.h>
99
#include <stdlib.h>
10+
#include <string.h>
1011

1112
#include <libtock-sync/interface/console.h>
1213
#include <libtock/kernel/ipc.h>
@@ -66,12 +67,8 @@ static int setup_logging() {
6667
static int log_to_screen(const char* message) {
6768
returncode_t ret;
6869

69-
// Copy up to the log buffer's size of the message, with room for a null byte.
70-
uint16_t len = strnlen(message, sizeof(log_buf) - 1);
71-
memcpy(log_buf, message, len);
72-
73-
// Add the null byte.
74-
log_buf[len] = '\0';
70+
// Load the log buffer with our message
71+
strlcpy(log_buf, message, LOG_WIDTH);
7572

7673
// Start the logging process.
7774
ret = ipc_notify_service(screen_service);

examples/tutorials/root_of_trust/encryption_service_milestone_two/main.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "libtock/tock.h"
88
#include <stdio.h>
99
#include <stdlib.h>
10+
#include <string.h>
1011

1112
#include <libtock-sync/interface/console.h>
1213
#include <libtock/kernel/ipc.h>
@@ -63,12 +64,8 @@ static int setup_logging() {
6364
static int log_to_screen(const char* message) {
6465
returncode_t ret;
6566

66-
// Copy up to the log buffer's size of the message, with room for a null byte.
67-
uint16_t len = strnlen(message, sizeof(log_buf) - 1);
68-
memcpy(log_buf, message, len);
69-
70-
// Add the null byte.
71-
log_buf[len] = '\0';
67+
// Load the log buffer with our message
68+
strlcpy(log_buf, message, LOG_WIDTH);
7269

7370
// Start the logging process.
7471
ret = ipc_notify_service(screen_service);

examples/tutorials/root_of_trust/suspicious_service_milestone_one/main.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "libtock/tock.h"
77
#include <stdio.h>
88
#include <stdlib.h>
9+
#include <string.h>
910

1011
#include <libtock/kernel/ipc.h>
1112

@@ -64,12 +65,8 @@ static int setup_logging() {
6465
static int log_to_screen(const char* message) {
6566
returncode_t ret;
6667

67-
// Copy up to the log buffer's size of the message, with room for a null byte.
68-
uint16_t len = strnlen(message, sizeof(log_buf) - 1);
69-
memcpy(log_buf, message, len);
70-
71-
// Add the null byte.
72-
log_buf[len] = '\0';
68+
// Load the log buffer with our message
69+
strlcpy(log_buf, message, LOG_WIDTH);
7370

7471
// Start the logging process.
7572
ret = ipc_notify_service(screen_service);

0 commit comments

Comments
 (0)