程序员

一个switch case引起的线上bug

作者:admin 2021-04-10 我要评论

?故障过程 1、上午的时候,QA同学突然说,测试自动化的流程突然过不去了,问我是不是最近对线上做了某些修改。当时第一反应是不可能 2、通过QA同学提供的test ca...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

?故障过程

1、上午的时候,QA同学突然说,测试自动化的流程突然过不去了,问我是不是最近对线上做了某些修改。当时第一反应是不可能

2、通过QA同学提供的test case,在测试环境通过curl发送请求,发现果然广告返回值跟预期不符。

3、通过git log对比,发现近期只有一个switch语句有修改。

4、尝试在代码中加入log语句,发现日志输出果然跟QA的错误结果一致,至此原因找到。

故障原因

下面是错误代码

switch?(dsp_res->bid_type())?{
????????case?0:
????????{
??????????auto?info?=?dsp_response->add_dsp_res_infos();
??????????info->set_dsp_id(item.dsp_id());
??????????if?(dsp_res->has_cache_duration())?{
????????????info->set_duration(dsp_res->cache_duration());
??????????}
??????????if?(dsp_res->has_quality())?{
????????????info->set_ratio(dsp_res->quality());
??????????}
????//?do?sth
????????}
????????case?1:
??????????break;
????????case?2:
????????{
??????????auto?info?=?dsp_response->add_dsp_res_infos();
??????????if?(dsp_res->has_cache_duration())?{
????????????info->set_duration(dsp_res->cache_duration());
??????????}

??????????if?(dsp_res->has_quality())?{
????????????info->set_ratio(dsp_res->quality());
??????????}

??????????info->set_dsp_id(item.dsp_id());
??????????std::unordered_set<std::string>?adids;
??????????for?(auto?elem?:?dsp_res->ad_ids())?{
????????????adids.insert(elem);
??????????}
????//?do?sth
??????????
????????}
????????case?3:?//?此case为新增
????????{
??????????auto?info?=?dsp_response->add_dsp_res_infos();
??????????info->set_dsp_id(item.dsp_id());
??????????if?(dsp_res->has_cache_duration())?{
????????????info->set_duration(dsp_res->cache_duration());
??????????}
??????????if?(dsp_res->has_quality())?{
????????????info->set_ratio(dsp_res->quality());
??????????}
????//?do?sth
????????}
????????default:
??????????break;
??????}

发现,当dsp_res->bid_type() == 2的时候,也会执行 case 3的部分,然后尝试在上面各个"do sth" 后面,加上break,结果符合预期,bug搞定。

深思

为什么在未增加新case之前,test case能通过呢?仔细找QA问了下case的逻辑,原来,case每次都会返回bid_type = 2。此处,我们再贴一次之前的代码:

switch?(dsp_res->bid_type())?{
????????case?0:
????????{
??????????auto?info?=?dsp_response->add_dsp_res_infos();
??????????info->set_dsp_id(item.dsp_id());
??????????if?(dsp_res->has_cache_duration())?{
????????????info->set_duration(dsp_res->cache_duration());
??????????}
??????????if?(dsp_res->has_quality())?{
????????????info->set_ratio(dsp_res->quality());
??????????}
????//?do?sth
????????}
????????case?1:
??????????break;
????????case?2:
????????{
??????????auto?info?=?dsp_response->add_dsp_res_infos();
??????????if?(dsp_res->has_cache_duration())?{
????????????info->set_duration(dsp_res->cache_duration());
??????????}

??????????if?(dsp_res->has_quality())?{
????????????info->set_ratio(dsp_res->quality());
??????????}

??????????info->set_dsp_id(item.dsp_id());
??????????std::unordered_set<std::string>?adids;
??????????for?(auto?elem?:?dsp_res->ad_ids())?{
????????????adids.insert(elem);
??????????}
????//?do?sth
????????}
????????default:
??????????break;
??????}

由于switch每次都会进入case 2的子逻辑,该逻辑后面就是default,然后break,没问题。但是增加了新case 3之后,因为case 2 和 case 3后面都没有break,导致会把case 2 和 case 3的代码都执行了,直到退出循环或者遇到break。此处列下switch case的三个规则:switch...case的三个规则:

  1. 既无成功匹配,又无default子句,那么swtich语句块什么也不做;
  2. 无成功匹配,但有default,那么swtich语句块做default语句块的事;
  3. 有成功匹配,没有break,那么成功匹配后,一直执行,直到遇到break。

看来我们的线上bug是因为遇到了第三个规则导致。

扩展

语句体中不包含break

#include?<stdio.h>
int?main()
{
????int?iChoice?=?0;
????printf("Enter?your?choice?=?");
????scanf(?"%d",&iChoice);
????switch?(iChoice)
????{
????case?1:
????????printf("case?1?!\n");
????case?2:
????????printf("case?2?!\n");
????case?3:
????????printf("case?3?!\n");
????default:
????????printf("default?!\n"?);
????}
????return?0;
}

当输入choice 为 1的时候

当输入choice 为 2的时候

原因:

在上面的示例中,如果iChoice等于1,则执行主体的所有语句,因为在开关主体中没有出现break语句。如果ichoice等于2,则由于没有break语句,因此控制跳至情况2并执行以下所有情况。

一个执行语句被多个case命中

void?TestFunction(void)
{
????printf("Demo?code\n");
}
int?main()
{
????int?iChoice?=?0;
????printf("Enter?your?choice?=?");
????scanf(?"%d",?&iChoice);
????switch?(?iChoice?)
????{
????case?1:
????case?2:
????case?3:
????????//Calling?function
????????TestFunction();
????????break;
????case?4:
????????printf("Wow?Test?paas?!");
????????break;
????default:
????????printf("default?!\n"?);
????}
????return?0;
}

输出为

原因:

对于case 1 2 3,都会执行到TestFunction

存在一样的case标签

#include?<stdio.h>
int?main()
{
????int?iChoice???=?0;
????int?i?=?0;
????printf("Enter?your?choice?=?");
????scanf(?"%d",?&iChoice);
????switch?(?iChoice?)
????{
????case?1:
????????i++;
????????break;
????case?3:
????????i?=?i?+?2;
????????break;
????case?3:
????????i?=?i?+?3;
????????break;
????default:
????????printf("default?!\n"?);
????????break;
????}
????printf("Value?of?i?=?%d",i);
????return?0;
}

输出

原因

case标签不能重复,否则编译器不能确定进入哪个标签

case值为浮点数

#include?<stdio.h>
int?main()
{
????int?iChoice???=?0;
????int?i?=?0;
????printf("Enter?your?choice?=?");
????scanf(?"%d",?&iChoice);
????switch?(iChoice)
????{
????case?1:
????????i++;
????????break;
????case?2.5:
????????i?=?i?+?2;
????????break;
????case?3:
????????i?=?i?+?3;
????????break;
????default:
????????printf("default?!\n"?);
????}
????printf("Value?of?i?=?%d",i);
????return?0;
}

输出:

原因:

switch 中的参数必须可以转换成一个整数

将default 语句放在正文的其他地方

#include?<stdio.h>
int?main()
{
????int?iChoice??=?0;
????printf("Enter?your?choice?=?");
????scanf(?"%d",?&iChoice);
????switch?(iChoice)
????{
????default:
????????printf("Bad?Input?!\n");
????????break;
????case?1:
????????printf("Your?enter?choice?is?1\n");
????????break;
????case?2:
????????printf("Your?enter?choice?is?2\n");
????????break;
????case?3:
????????printf("Your?enter?choice?is?3\n");
????????break;
????}
????return?0;
}

输出:

case标签值必须为常量

#include?<stdio.h>
int?main()
{
????int?iChoice??=?0;
????int?Label?=?1;
????printf("Enter?your?choice?=?");
????scanf(?"%d",?&iChoice);
????switch?(iChoice)
????{
????case?Label:
????????printf("Your?enter?choice?is?1\n");
????????break;
????case?2:
????????printf("Your?enter?choice?is?2\n");
????????break;
????case?3:
????????printf("Your?enter?choice?is?3\n");
????????break;
????default:
????????printf("Bad?Input?!\n");
????????break;
????}
????return?0;
}

输出:

嵌套开关

#include?<stdio.h>
void?nestedSwitchDemo(int?input1,?int?input2)
{
????switch?(input1)
????{
????case?1:
????????printf("Your?enter?choice?is?1\n");
????????switch?(input2?)
????????{
????????case?1:
????????????printf("Enter?Sub?choice?is?1\n");
????????????break;
????????case?2:
????????????printf("Enter?Sub?choice?is?2\n");
????????????break;
????????}
????????break;
????case?2:
????????printf("Your?enter?choice?is?2\n");
????????break;
????case?3:
????????printf("Your?enter?choice?is?3\n");
????????break;
????default:
????????printf("Bad?Input?!\n");
????????break;
????}
}
int?main()
{
????int?iChoice??=?1;
????int?iSubChoice?=?1;
????//Calling?function
????nestedSwitchDemo(iChoice,iSubChoice);
????return?0;
}

输出:

心得

平时编码中,一定要注意编码规范,每个case都写好对应的break,不要学习这种骚操作,稍不注意就可能出现线上故障。

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • Linux火眼金睛:查找两个目录之间的差

    Linux火眼金睛:查找两个目录之间的差

  • 微软提前推送Windows 10 21H1更新:这

    微软提前推送Windows 10 21H1更新:这

  • 学会这21条,你离Vim大神就不远了!

    学会这21条,你离Vim大神就不远了!

  • 有了这个神器,再也不怕shell写得不对

    有了这个神器,再也不怕shell写得不对

腾讯云代理商
精彩导读
海外云服务器
热门资讯
腾讯云代理商