dcerpc: remove use-after-free (found by clang).
[metze/wireshark/wip.git] / epan / tvbuff_real.c
1 /* tvbuff_real.c
2  *
3  * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #include "tvbuff.h"
27 #include "tvbuff-int.h"
28 #include "proto.h"      /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
29 #include "exceptions.h"
30
31 struct tvb_real {
32         struct tvbuff tvb;
33
34         /** Func to call when actually freed */
35         tvbuff_free_cb_t        free_cb;
36 };
37
38 static void
39 real_free(tvbuff_t *tvb)
40 {
41         struct tvb_real *real_tvb = (struct tvb_real *) tvb;
42
43         if (real_tvb->free_cb) {
44                 /*
45                  * XXX - do this with a union?
46                  */
47                 real_tvb->free_cb((gpointer)tvb->real_data);
48         }
49 }
50
51 static guint
52 real_offset(const tvbuff_t *tvb _U_, const guint counter)
53 {
54         return counter;
55 }
56
57 static const struct tvb_ops tvb_real_ops = {
58         sizeof(struct tvb_real), /* size */
59
60         real_free,            /* free */
61         real_offset,          /* offset */
62         NULL,                 /* get_ptr */
63         NULL,                 /* memcpy */
64         NULL,                 /* find_guint8 */
65         NULL,                 /* pbrk_guint8 */
66         NULL,                 /* clone */
67 };
68
69 tvbuff_t *
70 tvb_new_real_data(const guint8* data, const guint length, const gint reported_length)
71 {
72         tvbuff_t *tvb;
73         struct tvb_real *real_tvb;
74
75         THROW_ON(reported_length < -1, ReportedBoundsError);
76
77         tvb = tvb_new(&tvb_real_ops);
78
79         tvb->real_data       = data;
80         tvb->length          = length;
81         tvb->reported_length = reported_length;
82         tvb->initialized     = TRUE;
83
84         /*
85          * This is the top-level real tvbuff for this data source,
86          * so its data source tvbuff is itself.
87          */
88         tvb->ds_tvb = tvb;
89
90         real_tvb = (struct tvb_real *) tvb;
91         real_tvb->free_cb = NULL;
92
93         return tvb;
94 }
95
96 void
97 tvb_set_free_cb(tvbuff_t *tvb, const tvbuff_free_cb_t func)
98 {
99         struct tvb_real *real_tvb = (struct tvb_real *) tvb;
100
101         DISSECTOR_ASSERT(tvb);
102         DISSECTOR_ASSERT(tvb->ops == &tvb_real_ops);
103         real_tvb->free_cb = func;
104 }
105
106 void
107 tvb_set_child_real_data_tvbuff(tvbuff_t *parent, tvbuff_t *child)
108 {
109         DISSECTOR_ASSERT(parent && child);
110         DISSECTOR_ASSERT(parent->initialized);
111         DISSECTOR_ASSERT(child->initialized);
112         DISSECTOR_ASSERT(child->ops == &tvb_real_ops);
113         tvb_add_to_chain(parent, child);
114 }
115
116 tvbuff_t *
117 tvb_new_child_real_data(tvbuff_t *parent, const guint8* data, const guint length, const gint reported_length)
118 {
119         tvbuff_t *tvb = tvb_new_real_data(data, length, reported_length);
120
121         tvb_set_child_real_data_tvbuff(parent, tvb);
122
123         return tvb;
124 }
125
126 /*
127  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
128  *
129  * Local variables:
130  * c-basic-offset: 8
131  * tab-width: 8
132  * indent-tabs-mode: t
133  * End:
134  *
135  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
136  * :indentSize=8:tabSize=8:noTabs=false:
137  */