wireshark 是如何避免段错误的呢?
下列章节选自 README.developer 开发者手册的第 3 点。
前言
翻译完本小结,总结的很好,但是缺失简单的例子,这需要在阅读源码时留意本小结对 wireshark 源码的指导实践,。
3. Robustness.
3. 健壮性
Wireshark is not guaranteed to read only network traces that contain correctly-
formed packets. Wireshark is commonly used to track down networking
problems, and the problems might be due to a buggy protocol implementation
sending out bad packets.
Wireshark不能保证只读取包含正确格式数据包的网络跟踪文件。Wireshark通常用于跟踪网络问题,而这些问题可能是由于有错误的协议实现发送了错误的数据包导致的。
Therefore, code does not only have to be able to handle
correctly-formed packets without, for example, crashing or looping
infinitely, they also have to be able to handle *incorrectly*-formed
packets without crashing or looping infinitely.
因此,代码不仅需要能够处理正确格式的数据包,比如避免崩溃或无限循环,还需要能够处理不正确格式的数据包,同样也要避免崩溃或无限循环。
//即 wireshark 在处理错误格式的数据包时如何避免段错误和死循环呢?
Here are some suggestions for making code more robust in the face
of incorrectly-formed packets:
以下是一些使代码在面对格式不正确的数据包时更加健壮的建议:
Do *NOT* use "ws_assert()" or "ws_assert_not_reached()" with input data in dissectors.
*NO* value in a packet's data should be considered "wrong" in the sense
that it's a problem with the dissector if found; if it cannot do
anything else with a particular value from a packet's data, the
dissector should put into the protocol tree an indication that the
value is invalid, and should return. The "expert" mechanism should be
used for that purpose.
在解剖器中不要使用“ws_assert()”或“ws_assert_not_reached()”来处理输入数据。 不要认为数据包中的任何值都是“错误”的(译者注:因为所有可能的值都应该被视为有效的输入),如果发现一个值无法使用,解剖器应该在协议树中放置一个指示,表明该值是无效的,并返回。为此,应该使用“expert”机制,而不是使用断言。
//"NO" 表示“没有一个值”,意思是“没有一个数据包的数据值应该被视为“错误”(即有问题的)”,因为所有可能的值都应该被视为有效的输入。
Use assertions to catch logic errors in your program. A failed assertion
indicates a bug in the code. Use ws_assert() instead of g_assert() to
test a logic condition. Note that ws_assert() will be removed with
WS_DISABLE_ASSERT. Therefore assertions should not have any side-effects,
otherwise the program may behave inconsistently.
使用断言来捕获程序中的逻辑错误。断言失败表明代码中存在错误。使用ws_assert()来测试逻辑条件,而不是使用g_assert()。请注意,ws_assert()将被WS_DISABLE_ASSERT移除。因此,断言不应具有任何副作用,否则程序可能表现不一致。
//在代码中搜索, ws_assert() 断言用在非 packet-*.c 的代码文件中,这里的解剖器指的是packet-*.c 文件。
Use ws_assert_not_reached() instead of g_assert_not_reached() for
unreachable error conditions. For example if (and only if) you know
'myvar' can only have the values 1 and 2 do:
对于无法到达的错误情况,使用 ws_assert_not_reached() 而不是 g_assert_not_reached() 。 例如,如果(且仅当)您知道“myvar”只能具有值 1 和 2 时:
switch(myvar) {
case 1:
(...)
&n

Wireshark通过不使用ws_assert()或ws_assert_not_reached()来处理解剖器中的输入数据,而是使用DISSECTOR_ASSERT()等方法来确保在遇到错误格式的数据包时不会崩溃或无限循环。文章强调了代码应能处理不正确格式的数据包,并提供了一些最佳实践,包括使用断言来检测逻辑错误,利用tvb_get_*_string()等函数进行安全的数据提取,以及在循环和内存分配中进行边界检查以防止溢出和无限循环。
最低0.47元/天 解锁文章
1900

被折叠的 条评论
为什么被折叠?



