From 4f1a02909b8694dcc30fd5c7c6772fcfa1092ed9 Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Sat, 26 Jun 2021 12:21:19 +0200 Subject: lib: add sys_io_ranges_overlap() BUG: https://bugzilla.samba.org/show_bug.cgi?id=12033 Signed-off-by: Ralph Boehme Reviewed-by: Jeremy Allison --- lib/util/sys_rw.c | 25 ++++++++++ lib/util/sys_rw.h | 2 + lib/util/tests/test_sys_rw.c | 110 +++++++++++++++++++++++++++++++++++++++++++ lib/util/wscript_build | 6 +++ 4 files changed, 143 insertions(+) create mode 100644 lib/util/tests/test_sys_rw.c (limited to 'lib') diff --git a/lib/util/sys_rw.c b/lib/util/sys_rw.c index d74395fc409..02aaa871a39 100644 --- a/lib/util/sys_rw.c +++ b/lib/util/sys_rw.c @@ -48,6 +48,31 @@ bool sys_valid_io_range(off_t offset, size_t length) return true; } +bool sys_io_ranges_overlap(size_t c1, off_t o1, + size_t c2, off_t o2) +{ + if (c1 == 0 || c2 == 0) { + return false; + } + if (o2 < o1) { + /* + * o1 + * |····c1····| + * o2 + * |····c2···| ? + */ + return (o2 + c2 > o1); + } else { + /* + * o1 + * |····c1···| + * o2 + * |····c2····| ? + */ + return (o1 + c1 > o2); + } +} + /******************************************************************* A read wrapper that will deal with EINTR/EWOULDBLOCK ********************************************************************/ diff --git a/lib/util/sys_rw.h b/lib/util/sys_rw.h index b224ecb30ac..6693d34de57 100644 --- a/lib/util/sys_rw.h +++ b/lib/util/sys_rw.h @@ -28,6 +28,8 @@ struct iovec; bool sys_valid_io_range(off_t offset, size_t length); +bool sys_io_ranges_overlap(size_t c1, off_t o1, + size_t c2, off_t o2); ssize_t sys_read(int fd, void *buf, size_t count); void sys_read_v(int fd, void *buf, size_t count); ssize_t sys_write(int fd, const void *buf, size_t count); diff --git a/lib/util/tests/test_sys_rw.c b/lib/util/tests/test_sys_rw.c new file mode 100644 index 00000000000..551a6a09bda --- /dev/null +++ b/lib/util/tests/test_sys_rw.c @@ -0,0 +1,110 @@ +/* + * Unix SMB/CIFS implementation. + * + * Unit test for sys_rw.c + * + * Copyright (C) Ralph Böhme 2021 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include +#include +#include +#include +#include + +#include "lib/replace/replace.h" +#include "system/dir.h" + +#include "lib/util/sys_rw.c" + +static void test_sys_io_ranges_overlap(void **state) +{ + bool overlap; + + /* + * sys_io_ranges_overlap() args are: + * + * src size, src offset, dst size, dst offset + */ + + /* src and dst size 0 => no overlap */ + overlap = sys_io_ranges_overlap(0, 0, 0, 0); + assert_false(overlap); + + /* dst size 0 => no overlap */ + overlap = sys_io_ranges_overlap(1, 0, 0, 0); + assert_false(overlap); + + /* src size 0 => no overlap */ + overlap = sys_io_ranges_overlap(0, 0, 1, 0); + assert_false(overlap); + + /* same range => overlap */ + overlap = sys_io_ranges_overlap(1, 0, 1, 0); + assert_true(overlap); + + /* + * |.| + * |.| + * src before dst => no overlap + */ + overlap = sys_io_ranges_overlap(1, 0, 1, 1); + assert_false(overlap); + + /* + * |..| + * |..| + * src into dst => overlap + */ + overlap = sys_io_ranges_overlap(2, 0, 2, 1); + assert_true(overlap); + + /* + * |....| + * |..| + * src encompasses dst => overlap + */ + overlap = sys_io_ranges_overlap(4, 0, 1, 2); + assert_true(overlap); + + + /* + * |..| + * |..| + * dst into src => overlap + */ + overlap = sys_io_ranges_overlap(2, 1, 2, 0); + assert_true(overlap); + + /* + * |..| + * |....| + * dst encompasses src => overlap + */ + overlap = sys_io_ranges_overlap(2, 1, 4, 0); + assert_true(overlap); +} + +int main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_sys_io_ranges_overlap), + }; + + cmocka_set_message_output(CM_OUTPUT_SUBUNIT); + + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/lib/util/wscript_build b/lib/util/wscript_build index 2a99e212c5c..b8cfddb3f41 100644 --- a/lib/util/wscript_build +++ b/lib/util/wscript_build @@ -356,3 +356,9 @@ else: deps='cmocka replace talloc samba-util', local_include=False, for_selftest=True) + + bld.SAMBA_BINARY('test_sys_rw', + source='tests/test_sys_rw.c', + deps='cmocka replace samba-util', + local_include=False, + for_selftest=True) -- cgit v1.2.1