1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:07:34 +00:00

Kernel: Prefer snprintf over sprintf

This commit is contained in:
Ben Wiederhake 2020-08-16 07:51:56 +02:00 committed by Andreas Kling
parent 0240baa42d
commit b6afe1f0ce
2 changed files with 6 additions and 6 deletions

View file

@ -688,8 +688,8 @@ static Optional<KBuffer> procfs$pid_root(InodeIdentifier identifier)
static Optional<KBuffer> procfs$self(InodeIdentifier) static Optional<KBuffer> procfs$self(InodeIdentifier)
{ {
char buffer[16]; char buffer[16];
sprintf(buffer, "%d", Process::current()->pid().value()); int written = snprintf(buffer, sizeof(buffer), "%d", Process::current()->pid().value());
return KBuffer::copy((const u8*)buffer, strlen(buffer)); return KBuffer::copy((const u8*)buffer, static_cast<size_t>(written));
} }
Optional<KBuffer> procfs$mm(InodeIdentifier) Optional<KBuffer> procfs$mm(InodeIdentifier)
@ -1272,7 +1272,7 @@ KResult ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntr
} }
for (auto pid_child : Process::all_pids()) { for (auto pid_child : Process::all_pids()) {
char name[16]; char name[16];
size_t name_length = (size_t)sprintf(name, "%d", pid_child.value()); size_t name_length = (size_t)snprintf(name, sizeof(name), "%d", pid_child.value());
callback({ { name, name_length }, to_identifier(fsid(), PDI_Root, pid_child, FI_PID), 0 }); callback({ { name, name_length }, to_identifier(fsid(), PDI_Root, pid_child, FI_PID), 0 });
} }
break; break;
@ -1317,7 +1317,7 @@ KResult ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntr
if (!description) if (!description)
continue; continue;
char name[16]; char name[16];
size_t name_length = (size_t)sprintf(name, "%d", i); size_t name_length = (size_t)snprintf(name, sizeof(name), "%d", i);
callback({ { name, name_length }, to_identifier_with_fd(fsid(), pid, i), 0 }); callback({ { name, name_length }, to_identifier_with_fd(fsid(), pid, i), 0 });
} }
} break; } break;
@ -1330,7 +1330,7 @@ KResult ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntr
process->for_each_thread([&](Thread& thread) -> IterationDecision { process->for_each_thread([&](Thread& thread) -> IterationDecision {
int tid = thread.tid().value(); int tid = thread.tid().value();
char name[16]; char name[16];
size_t name_length = (size_t)sprintf(name, "%d", tid); size_t name_length = (size_t)snprintf(name, sizeof(name), "%d", tid);
callback({ { name, name_length }, to_identifier_with_stack(fsid(), tid), 0 }); callback({ { name, name_length }, to_identifier_with_stack(fsid(), tid), 0 });
return IterationDecision::Continue; return IterationDecision::Continue;
}); });

View file

@ -38,7 +38,7 @@ SlavePTY::SlavePTY(MasterPTY& master, unsigned index)
, m_master(master) , m_master(master)
, m_index(index) , m_index(index)
{ {
sprintf(m_tty_name, "/dev/pts/%u", m_index); snprintf(m_tty_name, sizeof(m_tty_name), "/dev/pts/%u", m_index);
auto process = Process::current(); auto process = Process::current();
set_uid(process->uid()); set_uid(process->uid());
set_gid(process->gid()); set_gid(process->gid());