summaryrefslogtreecommitdiff
path: root/lib/utils
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2017-04-20 11:24:05 -0700
committerScott Shawcroft <scott.shawcroft@gmail.com>2017-04-20 11:24:05 -0700
commit939c0045dbf2694a12432ad861db166ecf483b67 (patch)
tree79a3c953981101d240fe8256879b19ed1da8486a /lib/utils
parent076ff82c46a29ce899cb72a88f568f967f109bd7 (diff)
Switch to a shared piece of code to compute start and length of a
buffer from start, end and length. The old code miscomputed length leading to writing and reading from memory past the end of the buffer. Consolidating the code should make it easier to get right everywhere.
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/buffer_helper.c43
-rw-r--r--lib/utils/buffer_helper.h34
2 files changed, 77 insertions, 0 deletions
diff --git a/lib/utils/buffer_helper.c b/lib/utils/buffer_helper.c
new file mode 100644
index 000000000..f6eb8bf1e
--- /dev/null
+++ b/lib/utils/buffer_helper.c
@@ -0,0 +1,43 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "lib/utils/buffer_helper.h"
+
+void normalize_buffer_bounds(int32_t* start, int32_t end, uint32_t* length) {
+ if (end < 0) {
+ end += *length;
+ } else if (((uint32_t) end) > *length) {
+ end = *length;
+ }
+ if (*start < 0) {
+ *start += *length;
+ }
+ if (end < *start) {
+ *length = 0;
+ } else {
+ *length = end - *start;
+ }
+}
diff --git a/lib/utils/buffer_helper.h b/lib/utils/buffer_helper.h
new file mode 100644
index 000000000..5c4a8cb1e
--- /dev/null
+++ b/lib/utils/buffer_helper.h
@@ -0,0 +1,34 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef __MICROPY_INCLUDED_LIB_UTILS_BUFFER_HELPER_H__
+#define __MICROPY_INCLUDED_LIB_UTILS_BUFFER_HELPER_H__
+
+#include <stdint.h>
+
+void normalize_buffer_bounds(int32_t* start, int32_t end, uint32_t* length);
+
+#endif // __MICROPY_INCLUDED_LIB_UTILS_BUFFER_HELPER_H__