Skip to content

Commit

Permalink
o Code cleanup. Convert strncpy to strlcpy mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Miller committed Nov 22, 2023
1 parent 75adfd6 commit 73395b6
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 19 deletions.
2 changes: 2 additions & 0 deletions app/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ - (intptr_t)boot {
generic_mkdirat(AT_PWD, "/run", 0755);
generic_unlinkat(AT_PWD, "/var/run");
generic_symlinkat("/run", AT_PWD, "/var/run");
// The following would be a better solution for /var/run, but tmpfs is currently broken
//do_mount(&tmpfs, "run", "/run", "", 0);

// Create directories/links to simulate /sys stuff for battery monitoring
generic_mkdirat(AT_PWD, "/sys/class", 0755);
Expand Down
2 changes: 1 addition & 1 deletion app/PasteboardDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static ssize_t clipboard_read_sync(clip_fd *fd) {
}

@autoreleasepool {
size_t len;
size_t len = 0;
const void *data = get_data(fd, &len);

// Make sure size is still INITIAL_BUFFER_CAP based
Expand Down
2 changes: 1 addition & 1 deletion fs/fake.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ static int fakefs_mount(struct mount *mount) {
strncpy(db_path, mount->source, PATH_MAX -1);
char *basename = strrchr(db_path, '/') + 1;
assert(strcmp(basename, "data") == 0);
strncpy(basename, "meta.db", 8);
strlcpy(basename, "meta.db", 8);

// do this now so rebuilding can use root_fd
int err = realfs.mount(mount);
Expand Down
6 changes: 3 additions & 3 deletions fs/lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static struct file_lock *file_lock_copy(struct file_lock *request) {
lock->type = request->type;
lock->owner = request->owner;
lock->pid = request->pid;
strncpy(lock->comm, request->comm, 16);
strlcpy(lock->comm, request->comm, 16);
list_init(&lock->locks);
return lock;
}
Expand Down Expand Up @@ -176,7 +176,7 @@ static int file_lock_from_flock(struct fd *fd, struct flock_ *flock, struct file
lock->type = flock->type;
lock->owner = current->files;
lock->pid = current->pid;
strncpy(lock->comm, current->comm, 16);
strlcpy(lock->comm, current->comm, 16);
return 0;
}

Expand All @@ -189,7 +189,7 @@ static int flock_from_file_lock(struct file_lock *lock, struct flock_ *flock) {
else
flock->len = 0;
flock->pid = lock->pid;
strncpy(lock->comm, flock->comm, 16);
strlcpy(lock->comm, flock->comm, 16);
return 0;
}

Expand Down
10 changes: 5 additions & 5 deletions fs/proc/ish.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ char *get_ip_str(const struct sockaddr *sa, char *s, socklen_t maxlen) {
break;

default:
strncpy(s, "Unknown AF", maxlen);
strlcpy(s, "Unknown AF", maxlen);
return NULL;
}

Expand Down Expand Up @@ -204,7 +204,7 @@ char *parse_if_flags(int flags) {
{IFF_MULTICAST, "MULTICAST"},
};

for (int i = 0; i < sizeof(flag_str_map)/sizeof(flag_str_map[0]); ++i) {
for (unsigned long i = 0; i < sizeof(flag_str_map)/sizeof(flag_str_map[0]); ++i) {
if (flags & flag_str_map[i].flag) {
if (!first) {
strcat(build_string, ",");
Expand Down Expand Up @@ -235,9 +235,9 @@ static int proc_ish_show_ips(struct proc_entry *UNUSED(entry), struct proc_data
char int_dstaddr[100];

if (cursor->ifa_addr->sa_family == AF_INET) {
strncpy(type, "IF_INET", sizeof(type));
strlcpy(type, "IF_INET", sizeof(type));
} else {
strncpy(type, "IF_INET6", sizeof(type));
strlcpy(type, "IF_INET6", sizeof(type));
}
type[sizeof(type) - 1] = '\0';

Expand All @@ -250,7 +250,7 @@ static int proc_ish_show_ips(struct proc_entry *UNUSED(entry), struct proc_data
}

char int_flags[250];
strncpy(int_flags, parse_if_flags(cursor->ifa_flags), sizeof(int_flags));
strlcpy(int_flags, parse_if_flags(cursor->ifa_flags), sizeof(int_flags));
int_flags[sizeof(int_flags) - 1] = '\0';

proc_printf(buf, "%-10.10s %-40s %-40s %-8s %-60s\n",
Expand Down
2 changes: 1 addition & 1 deletion fs/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static struct fd *sock_getfd(fd_t sock_fd) {
return sock;
}

static uint32_t unix_socket_next_id() {
static uint32_t unix_socket_next_id(void) {
static uint32_t next_id = 0;
static lock_t next_id_lock = LOCK_INITIALIZER;
lock(&next_id_lock, 0);
Expand Down
2 changes: 1 addition & 1 deletion kernel/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ int __do_execve(const char *file, struct exec_args argv, struct exec_args envp)
basename = file;
else
basename++;
strncpy(current->comm, basename, sizeof(current->comm));
strlcpy(current->comm, basename, sizeof(current->comm));
unlock(&current->general_lock);

update_thread_name();
Expand Down
2 changes: 1 addition & 1 deletion kernel/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ char * current_comm(void) {
static char comm[16];
if(current != NULL) {
if(strcmp(current->comm, "")) {
strncpy(comm, current->comm, 16);
strlcpy(comm, current->comm, 16);
} else {
return "";
}
Expand Down
12 changes: 6 additions & 6 deletions util/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ static inline void __lock(lock_t *lock, int log_lock, __attribute__((unused)) co
lock->owner = pthread_self();
lock->pid = current_pid();
lock->uid = current_uid();
strncpy(lock->comm, current_comm(), 16);
strlcpy(lock->comm, current_comm(), 16);
modify_critical_region_counter_wrapper(-1, __FILE__, __LINE__);
} else {
pthread_mutex_lock(&lock->m);
lock->owner = pthread_self();
lock->pid = current_pid();
lock->uid = current_uid();
strncpy(lock->comm, current_comm(), 16);
strlcpy(lock->comm, current_comm(), 16);
}
return;
}
Expand Down Expand Up @@ -343,7 +343,7 @@ static inline void __write_lock(wrlock_t *lock, const char *file, int line) { //
lock->line = line;
lock->pid = current_pid();
if(lock->pid > 9)
strncpy((char *)lock->comm, current_comm(), 16);
strlcpy((char *)lock->comm, current_comm(), 16);
//STRACE("write_lock(%x, %s(%d), %s, %d\n", lock, lock->comm, lock->pid, file, line);
}

Expand All @@ -370,7 +370,7 @@ static inline int trylockw(wrlock_t *lock, __attribute__((unused)) const char *f
modify_locks_held_count_wrapper(1);
//STRACE("trylockw(%x, %s(%d), %s, %d\n", lock, lock->comm, lock->pid, file, line);
lock->pid = current_pid();
strncpy(lock->comm, current_comm(), 16);
strlcpy(lock->comm, current_comm(), 16);
}
return status;
}
Expand All @@ -395,7 +395,7 @@ static inline int trylock(lock_t *lock, __attribute__((unused)) const char *file

//STRACE("trylock(%x, %s(%d), %s, %d\n", lock, lock->comm, lock->pid, file, line);
lock->pid = current_pid();
strncpy(lock->comm, current_comm(), 16);
strlcpy(lock->comm, current_comm(), 16);
}
return status;
}
Expand Down Expand Up @@ -499,7 +499,7 @@ static inline void _read_lock(wrlock_t *lock, __attribute__((unused)) const char

lock->pid = current_pid();
if(lock->pid > 9)
strncpy((char *)lock->comm, current_comm(), 16);
strlcpy((char *)lock->comm, current_comm(), 16);
modify_critical_region_counter_wrapper(-1, __FILE__, __LINE__);
//STRACE("read_lock(%d, %s(%d), %s, %d\n", lock, lock->comm, lock->pid, file, line);
}
Expand Down

0 comments on commit 73395b6

Please sign in to comment.