Skip to content

Commit 5ca921c

Browse files
simarkeepp
authored andcommitted
Fix: sink.text.pretty: check that port is connected before creating message iterator
sink.text.pretty does not check if its input port is connected before trying to create a message iterator on it. This can lead to a precondition assertion failure. It can be reproduced with this Python snippet. import bt2 g = bt2.Graph() g.add_component(bt2.find_plugin('text').sink_component_classes['pretty'], 'snk') g.run() The assertion failure we get is: 04-14 11:35:27.339 1231815 1231815 F LIB/MSG-ITER [email protected]:295 Babeltrace 2 library precondition not satisfied; error is: 04-14 11:35:27.339 1231815 1231815 F LIB/MSG-ITER [email protected]:295 Input port is not connected: port-addr=0x607000001d70, port-type=INPUT, port-name="in" 04-14 11:35:27.339 1231815 1231815 F LIB/MSG-ITER [email protected]:295 Aborting... ./tests/utils/../utils/utils.sh: line 283: 1231815 Aborted (core dumped) env "${env_args[@]}" "$@" Add a check and return an error if that happens instead. A corresponding test is also added. Change-Id: Ibeed94cd6ece543817fe8a765a69cb52bbaaba76 Signed-off-by: Simon Marchi <[email protected]> Reviewed-on: https://review.lttng.org/c/babeltrace/+/3403 Reviewed-by: Philippe Proulx <[email protected]> Tested-by: jenkins <[email protected]> Reviewed-on: https://review.lttng.org/c/babeltrace/+/3411
1 parent 2467456 commit 5ca921c

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

src/plugins/text/pretty/pretty.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,34 @@ bt_message_iterator_class_next_method_status handle_message(
139139

140140
BT_HIDDEN
141141
bt_component_class_sink_graph_is_configured_method_status
142-
pretty_graph_is_configured(bt_self_component_sink *comp)
142+
pretty_graph_is_configured(bt_self_component_sink *self_comp_sink)
143143
{
144144
bt_component_class_sink_graph_is_configured_method_status status;
145145
bt_message_iterator_create_from_sink_component_status
146146
msg_iter_status;
147147
struct pretty_component *pretty;
148+
bt_self_component *self_comp =
149+
bt_self_component_sink_as_self_component(self_comp_sink);
150+
const bt_component *comp = bt_self_component_as_component(self_comp);
151+
bt_self_component_port_input *in_port;
152+
bt_logging_level log_level = bt_component_get_logging_level(comp);
148153

149-
pretty = bt_self_component_get_data(
150-
bt_self_component_sink_as_self_component(comp));
154+
pretty = bt_self_component_get_data(self_comp);
151155
BT_ASSERT(pretty);
152156
BT_ASSERT(!pretty->iterator);
157+
158+
in_port = bt_self_component_sink_borrow_input_port_by_name(self_comp_sink,
159+
in_port_name);
160+
if (!bt_port_is_connected(bt_port_input_as_port_const(
161+
bt_self_component_port_input_as_port_input(in_port)))) {
162+
BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Single input port is not connected: "
163+
"port-name=\"%s\"", in_port_name);
164+
status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
165+
goto end;
166+
}
167+
153168
msg_iter_status = bt_message_iterator_create_from_sink_component(
154-
comp, bt_self_component_sink_borrow_input_port_by_name(comp,
155-
in_port_name), &pretty->iterator);
169+
self_comp_sink, in_port, &pretty->iterator);
156170
if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
157171
status = (int) msg_iter_status;
158172
goto end;

tests/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ dist_check_SCRIPTS = \
5353
cli/test_trace_read \
5454
cli/test_trimmer \
5555
plugins/sink.text.details/succeed/test_succeed \
56+
plugins/sink.text.pretty/test_pretty \
57+
plugins/sink.text.pretty/test_pretty.py \
5658
plugins/src.ctf.lttng-live/test_live \
5759
python-plugin-provider/bt_plugin_test_python_plugin_provider.py \
5860
python-plugin-provider/test_python_plugin_provider \
@@ -143,6 +145,7 @@ TESTS_PYTHON_PLUGIN_PROVIDER =
143145

144146
if ENABLE_PYTHON_PLUGINS
145147
TESTS_PYTHON_PLUGIN_PROVIDER += python-plugin-provider/test_python_plugin_provider
148+
TESTS_PLUGINS += plugins/sink.text.pretty/test_pretty
146149
if ENABLE_DEBUG_INFO
147150
TESTS_PLUGINS += \
148151
plugins/flt.lttng-utils.debug-info/test_succeed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
#
3+
# SPDX-License-Identifier: GPL-2.0-only
4+
#
5+
# Copyright (C) 2017 Philippe Proulx <[email protected]>
6+
#
7+
8+
if [ "x${BT_TESTS_SRCDIR:-}" != "x" ]; then
9+
UTILSSH="$BT_TESTS_SRCDIR/utils/utils.sh"
10+
else
11+
UTILSSH="$(dirname "$0")/../../utils/utils.sh"
12+
fi
13+
14+
# shellcheck source=../../utils/utils.sh
15+
source "$UTILSSH"
16+
17+
run_python_bt2_test "${BT_TESTS_SRCDIR}/plugins/sink.text.pretty" "test_*"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
#
3+
# Copyright (C) 2020 EfficiOS, Inc.
4+
5+
import unittest
6+
import bt2
7+
8+
9+
class Test(unittest.TestCase):
10+
# Test that the component returns an error if the graph is configured while
11+
# the component's input port is left disconnected.
12+
def test_unconnected_port_raises(self):
13+
graph = bt2.Graph()
14+
graph.add_component(
15+
bt2.find_plugin('text').sink_component_classes['pretty'], 'snk'
16+
)
17+
18+
with self.assertRaisesRegex(
19+
bt2._Error, 'Single input port is not connected: port-name="in"'
20+
):
21+
graph.run()
22+
23+
24+
if __name__ == '__main__':
25+
unittest.main()

0 commit comments

Comments
 (0)