summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/kernel/mem/virt.c21
-rw-r--r--src/kernel/proc.c3
-rw-r--r--src/kernel/util.h10
-rw-r--r--src/kernel/vfs/request.c7
4 files changed, 22 insertions, 19 deletions
diff --git a/src/kernel/mem/virt.c b/src/kernel/mem/virt.c
index a9450c7..1fa5a10 100644
--- a/src/kernel/mem/virt.c
+++ b/src/kernel/mem/virt.c
@@ -28,9 +28,9 @@ bool virt_iter_next(struct virt_iter *iter) {
if (partial <= 0) return false;
if (iter->_pages) { // if iterating over virtual memory
- // don't read past the page
- if (((uintptr_t)iter->_virt & PAGE_MASK) + partial > PAGE_SIZE)
- partial = PAGE_SIZE - ((uintptr_t)iter->_virt & PAGE_MASK);
+ // don't cross page boundaries
+ size_t to_page = PAGE_SIZE - ((uintptr_t)iter->_virt & PAGE_MASK);
+ partial = min(partial, to_page);
iter->frag = pagedir_virt2phys(iter->_pages,
iter->_virt, iter->_user, iter->_writeable);
@@ -56,7 +56,7 @@ bool virt_cpy(
struct pagedir *src_pages, const void __user *src, size_t length)
{
struct virt_iter dest_iter, src_iter;
- size_t min;
+ size_t cur_len;
virt_iter_new(&dest_iter, dest, length, dest_pages, true, true);
virt_iter_new( &src_iter, (userptr_t)src, length, src_pages, true, false);
@@ -69,14 +69,13 @@ bool virt_cpy(
if ( src_iter.frag_len <= 0)
if (!virt_iter_next( &src_iter)) break;
- min = src_iter.frag_len < dest_iter.frag_len
- ? src_iter.frag_len : dest_iter.frag_len;
- memcpy(dest_iter.frag, src_iter.frag, min);
+ cur_len = min(src_iter.frag_len, dest_iter.frag_len);
+ memcpy(dest_iter.frag, src_iter.frag, cur_len);
- dest_iter.frag_len -= min;
- dest_iter.frag += min;
- src_iter.frag_len -= min;
- src_iter.frag += min;
+ dest_iter.frag_len -= cur_len;
+ dest_iter.frag += cur_len;
+ src_iter.frag_len -= cur_len;
+ src_iter.frag += cur_len;
}
return !(dest_iter.error || src_iter.error);
diff --git a/src/kernel/proc.c b/src/kernel/proc.c
index 94fc419..13ce759 100644
--- a/src/kernel/proc.c
+++ b/src/kernel/proc.c
@@ -115,8 +115,7 @@ int process_try2collect(struct process *dead) {
dead->state = PS_DEADER;
parent->state = PS_RUNNING;
- len = parent->death_msg.len < dead->death_msg.len
- ? parent->death_msg.len : dead->death_msg.len;
+ len = min(parent->death_msg.len, dead->death_msg.len);
res = virt_cpy(
parent->pages, parent->death_msg.buf,
dead->pages, dead->death_msg.buf, len);
diff --git a/src/kernel/util.h b/src/kernel/util.h
index b50810d..839aa49 100644
--- a/src/kernel/util.h
+++ b/src/kernel/util.h
@@ -8,3 +8,13 @@ void *memcpy(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
+
+// see https://gcc.gnu.org/onlinedocs/gcc/Typeof.html
+#define min(a,b) ({ \
+ typeof (a) _a = (a); \
+ typeof (b) _b = (b); \
+ _a < _b ? _a : _b; })
+#define max(a,b) ({ \
+ typeof (a) _a = (a); \
+ typeof (b) _b = (b); \
+ _a > _b ? _a : _b; })
diff --git a/src/kernel/vfs/request.c b/src/kernel/vfs/request.c
index fcd6c47..9826982 100644
--- a/src/kernel/vfs/request.c
+++ b/src/kernel/vfs/request.c
@@ -47,12 +47,7 @@ _Noreturn void vfs_request_pass2handler(struct vfs_request *req) {
handler->state = PS_RUNNING;
handler->handled_req = req;
- len = handler->awaited_req.max_len;
- if (len > req->input.len) {
- // input bigger than buffer
- // TODO what should be done during e.g. open() calls? truncating doesn't seem right
- len = req->input.len;
- }
+ len = min(req->input.len, handler->awaited_req.max_len);
if (req->input.kern) {
if (!virt_cpy_to(handler->pages,