Fix: add stricter checks on packet boundaries
[babeltrace.git] / formats / ctf / types / integer.c
index 719823b9ff23675aab3a0f1b787ddeb8f0af25f2..189943e6052438bc4b6e3cb2895dcbfc54005562 100644 (file)
  *
  * 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 <babeltrace/ctf/types.h>
 #include <babeltrace/bitfield.h>
 #include <stdint.h>
 #include <glib.h>
-#include <endian.h>
+#include <babeltrace/endian.h>
 
 /*
  * The aligned read/write functions are expected to be faster than the
@@ -31,8 +39,8 @@
  */
 
 static
-int _aligned_integer_read(struct stream_pos *ppos,
-                         struct definition *definition)
+int _aligned_integer_read(struct bt_stream_pos *ppos,
+                         struct bt_definition *definition)
 {
        struct definition_integer *integer_definition =
                container_of(definition, struct definition_integer, p);
@@ -41,7 +49,8 @@ int _aligned_integer_read(struct stream_pos *ppos,
        struct ctf_stream_pos *pos = ctf_pos(ppos);
        int rbo = (integer_declaration->byte_order != BYTE_ORDER);      /* reverse byte order */
 
-       ctf_align_pos(pos, integer_declaration->p.alignment);
+       if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+               return -EFAULT;
 
        if (!ctf_pos_access_ok(pos, integer_declaration->len))
                return -EFAULT;
@@ -103,7 +112,7 @@ int _aligned_integer_read(struct stream_pos *ppos,
 
                        v = *(const int16_t *) ctf_get_pos_addr(pos);
                        integer_definition->value._signed =
-                               rbo ? GUINT16_SWAP_LE_BE(v) : v;
+                               rbo ? (int16_t) GUINT16_SWAP_LE_BE(v) : v;
                        break;
                }
                case 32:
@@ -112,7 +121,7 @@ int _aligned_integer_read(struct stream_pos *ppos,
 
                        v = *(const int32_t *) ctf_get_pos_addr(pos);
                        integer_definition->value._signed =
-                               rbo ? GUINT32_SWAP_LE_BE(v) : v;
+                               rbo ? (int32_t) GUINT32_SWAP_LE_BE(v) : v;
                        break;
                }
                case 64:
@@ -121,20 +130,21 @@ int _aligned_integer_read(struct stream_pos *ppos,
 
                        v = *(const int64_t *) ctf_get_pos_addr(pos);
                        integer_definition->value._signed =
-                               rbo ? GUINT64_SWAP_LE_BE(v) : v;
+                               rbo ? (int64_t) GUINT64_SWAP_LE_BE(v) : v;
                        break;
                }
                default:
                        assert(0);
                }
        }
-       ctf_move_pos(pos, integer_declaration->len);
+       if (!ctf_move_pos(pos, integer_declaration->len))
+               return -EFAULT;
        return 0;
 }
 
 static
-int _aligned_integer_write(struct stream_pos *ppos,
-                           struct definition *definition)
+int _aligned_integer_write(struct bt_stream_pos *ppos,
+                           struct bt_definition *definition)
 {
        struct definition_integer *integer_definition =
                container_of(definition, struct definition_integer, p);
@@ -143,7 +153,8 @@ int _aligned_integer_write(struct stream_pos *ppos,
        struct ctf_stream_pos *pos = ctf_pos(ppos);
        int rbo = (integer_declaration->byte_order != BYTE_ORDER);      /* reverse byte order */
 
-       ctf_align_pos(pos, integer_declaration->p.alignment);
+       if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+               return -EFAULT;
 
        if (!ctf_pos_access_ok(pos, integer_declaration->len))
                return -EFAULT;
@@ -182,12 +193,12 @@ int _aligned_integer_write(struct stream_pos *ppos,
                        break;
                case 16:
                        *(int16_t *) ctf_get_pos_addr(pos) = rbo ?
-                                                GUINT16_SWAP_LE_BE((int16_t) v) :
+                                                (int16_t) GUINT16_SWAP_LE_BE((int16_t) v) :
                                                 (int16_t) v;
                        break;
                case 32:
                        *(int32_t *) ctf_get_pos_addr(pos) = rbo ?
-                                                GUINT32_SWAP_LE_BE((int32_t) v) :
+                                                (int32_t) GUINT32_SWAP_LE_BE((int32_t) v) :
                                                 (int32_t) v;
                        break;
                case 64:
@@ -199,11 +210,12 @@ int _aligned_integer_write(struct stream_pos *ppos,
                }
        }
 end:
-       ctf_move_pos(pos, integer_declaration->len);
+       if (!ctf_move_pos(pos, integer_declaration->len))
+               return -EFAULT;
        return 0;
 }
 
-int ctf_integer_read(struct stream_pos *ppos, struct definition *definition)
+int ctf_integer_read(struct bt_stream_pos *ppos, struct bt_definition *definition)
 {
        struct definition_integer *integer_definition =
                container_of(definition, struct definition_integer, p);
@@ -216,35 +228,41 @@ int ctf_integer_read(struct stream_pos *ppos, struct definition *definition)
                return _aligned_integer_read(ppos, definition);
        }
 
-       ctf_align_pos(pos, integer_declaration->p.alignment);
+       if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+               return -EFAULT;
 
        if (!ctf_pos_access_ok(pos, integer_declaration->len))
                return -EFAULT;
 
        if (!integer_declaration->signedness) {
                if (integer_declaration->byte_order == LITTLE_ENDIAN)
-                       bt_bitfield_read_le(pos->base, unsigned long,
+                       bt_bitfield_read_le(mmap_align_addr(pos->base_mma) +
+                                       pos->mmap_base_offset, unsigned long,
                                pos->offset, integer_declaration->len,
                                &integer_definition->value._unsigned);
                else
-                       bt_bitfield_read_be(pos->base, unsigned long,
+                       bt_bitfield_read_be(mmap_align_addr(pos->base_mma) +
+                                       pos->mmap_base_offset, unsigned long,
                                pos->offset, integer_declaration->len,
                                &integer_definition->value._unsigned);
        } else {
                if (integer_declaration->byte_order == LITTLE_ENDIAN)
-                       bt_bitfield_read_le(pos->base, unsigned long,
+                       bt_bitfield_read_le(mmap_align_addr(pos->base_mma) +
+                                       pos->mmap_base_offset, unsigned long,
                                pos->offset, integer_declaration->len,
                                &integer_definition->value._signed);
                else
-                       bt_bitfield_read_be(pos->base, unsigned long,
+                       bt_bitfield_read_be(mmap_align_addr(pos->base_mma) +
+                                       pos->mmap_base_offset, unsigned long,
                                pos->offset, integer_declaration->len,
                                &integer_definition->value._signed);
        }
-       ctf_move_pos(pos, integer_declaration->len);
+       if (!ctf_move_pos(pos, integer_declaration->len))
+               return -EFAULT;
        return 0;
 }
 
-int ctf_integer_write(struct stream_pos *ppos, struct definition *definition)
+int ctf_integer_write(struct bt_stream_pos *ppos, struct bt_definition *definition)
 {
        struct definition_integer *integer_definition =
                container_of(definition, struct definition_integer, p);
@@ -257,7 +275,8 @@ int ctf_integer_write(struct stream_pos *ppos, struct definition *definition)
                return _aligned_integer_write(ppos, definition);
        }
 
-       ctf_align_pos(pos, integer_declaration->p.alignment);
+       if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+               return -EFAULT;
 
        if (!ctf_pos_access_ok(pos, integer_declaration->len))
                return -EFAULT;
@@ -266,24 +285,29 @@ int ctf_integer_write(struct stream_pos *ppos, struct definition *definition)
                goto end;
        if (!integer_declaration->signedness) {
                if (integer_declaration->byte_order == LITTLE_ENDIAN)
-                       bt_bitfield_write_le(pos->base, unsigned long,
+                       bt_bitfield_write_le(mmap_align_addr(pos->base_mma) +
+                                       pos->mmap_base_offset, unsigned long,
                                pos->offset, integer_declaration->len,
                                integer_definition->value._unsigned);
                else
-                       bt_bitfield_write_be(pos->base, unsigned long,
+                       bt_bitfield_write_be(mmap_align_addr(pos->base_mma) +
+                                       pos->mmap_base_offset, unsigned long,
                                pos->offset, integer_declaration->len,
                                integer_definition->value._unsigned);
        } else {
                if (integer_declaration->byte_order == LITTLE_ENDIAN)
-                       bt_bitfield_write_le(pos->base, unsigned long,
+                       bt_bitfield_write_le(mmap_align_addr(pos->base_mma) +
+                                       pos->mmap_base_offset, unsigned long,
                                pos->offset, integer_declaration->len,
                                integer_definition->value._signed);
                else
-                       bt_bitfield_write_be(pos->base, unsigned long,
+                       bt_bitfield_write_be(mmap_align_addr(pos->base_mma) +
+                                       pos->mmap_base_offset, unsigned long,
                                pos->offset, integer_declaration->len,
                                integer_definition->value._signed);
        }
 end:
-       ctf_move_pos(pos, integer_declaration->len);
+       if (!ctf_move_pos(pos, integer_declaration->len))
+               return -EFAULT;
        return 0;
 }
This page took 0.029087 seconds and 4 git commands to generate.