SlideShare a Scribd company logo
where does bug come from
Yu Shen
2017.6
1
experiences
2 years
200+ bug fixs
2
bugs can be deadly
ifย (launchย =ย true)
{
ย ย ย ย launch_missile();
}
3
bugs can be deadly
4
bugs can be deadly
a shell script to clean the TMPDIR.
rmย โ€rfย $TMPDIR/
5
bugs can be deadly
a script to clean the TMPDIR.
rmย โ€rfย $TMPDIR/*
but when ย $TMPDIRย is empty
6
if you see
core dump
freeze
unexpected error message
software behave in unintended ways
7
There is likey to be a bug...
8
all kinds of bugs
bad programming practice
dangerous C functions: strcmp, strcpy, strcat, sprintf ...
function is not declared before use
forgot to check input arguments
forgot to check function's return code
memory management
buffers overflow
free memory on stack
deโ€referencing/accessing NULL pointers
memory leak
9
bugs in catogories
concurrency
without locks
dead locks
TLS โ€ thread local storage
compiler optimization
buggy new features/codes
bad design
10
bad programming practice
example1: strcmp
11
strcmp
ifย (strcmp(teโ€>desc,ย "MATERIALIZEDย VIEWย DATA"))
{
...
click to see on gerrit
12
strcmp
ifย (strcmp(teโ€>desc,ย "MATERIALIZEDย VIEWย DATA")ย ==ย 0)
{
...
click to see on gerrit
13
bad programming practice
example2: function is not declared before use
14
function is not declared before use
#0ย strlenย ()
#1ย vfprintfย ()
#2ย vsnprintfย ()ย 
#3ย appendStringInfoVAย (str=0x7fffb327c000,ย 
ย ย ย fmt=0x3e87260ย "tableย %sย isย remappedย toย %sย byย mappingย ruleย "%s""
#4ย elog_finishย (elevel=13,ย fmt=0x4f9de0ย "tableย %sย isย remappedย toย %sย b
#5ย ApplyCatalogMappingRules
click to see on gerrit
15
function is not declared before use
charย *
ApplyCatalogMappingRules(
ย  constย charย *schema,ย constย charย *object)
{
ย ย rewritten_fqnameย =ย filter_RE_replace(fqname,ย li_entryโ€>re,ย ย li_entr
ย ย elog(DEBUG2,ย "tableย %sย isย remappedย toย %sย byย mappingย ruleย "%s""
(gdb)ย pย rewritten_fqnameย 
$2ย =ย 0xffffffffd53b9d90
ย ย ย ย <Addressย 0xffffffffd53b9d90ย outย ofย bounds>ย 
click to see on gerrit
16
function is not declared before use
(gdb)ย disasย filter_RE_replace
...
<filter_RE_replace+214>:ย ย ย ย callqย ย 0x45a54aย <text_to_cstring>ย 
<filter_RE_replace+219>:ย ย ย ย cltq
...
click to see on gerrit
17
function is not declared before use
if the function is not declared before use. the complier will assume
the return type of the function to be int32 which is not enough to
store 64bit memory address. and later use 'cltq' to extend the
int32๏ดพeax๏ดฟ to int64๏ดพrax๏ดฟ which is an illegal memory address.
to fix the bug, add declaration before use.
externย charย *text_to_cstring(constย textย *t);
click to see on gerrit
18
bad programming practice
example3: forget to check input arguments
19
forgot to check input arguments
sword
DCIBindArrayOfStruct(DCIBindย *bindp,ย DCIErrorย *errhp,ย 
ub4ย pvskip,ย ub4ย indskip,
ub4ย alskip,ย ub4ย rcskip)
{
ย  mylog("[DCIBindArrayOfStruct]:ย bindpย =ย %p,ย pvskipย =ย %u,ย indsk
ย  bindpโ€>pvskipย =ย pvskip;
ย  bindpโ€>indskipย =ย indskip;
...
20
forgot to check input arguments
sword
DCIBindArrayOfStruct(DCIBindย *bindp,ย DCIErrorย *errhp,ย 
ย  ย  ย  ย  ย  ย ub4ย pvskip,ย ub4ย indskip,
ย  ย  ย  ย  ย  ย ub4ย alskip,ย ub4ย rcskip)
{
ย ย ย ย ifย (NULLย ==ย bindpย ||
ย ย ย ย bindpโ€>head.handle_typeย !=ย DCI_HTYPE_BIND)
ย ย ย ย ย ย ย ย returnย DCI_INVALID_HANDLE;
...
21
bad programming practice
example4: forget to check input arguments
22
forget to check input arguments
builder@h1:~/manael/C_INTERFACE/src/odbc$ย isqlย โ€vย kingbase_s
[28000][unixODBC]Unexpectedย protocolย characterย during
authenticationย orย KingbaseESย hasย beenย closed;
Errorย whileย readingย toย theย socket.
[ISQL]ERROR:ย Couldย notย SQLConnect
23
forget to check input arguments
#defineย RETRY_TICKย ย 1000000ย ย ย /*ย inย microsecondsย */
intย 
SOCK_wait_for_ready(SocketClassย *sock,ย intย retry_count)
{
doย {
ย ย structย ย timevalย ย tm;
ย ย ifย (!no_timeout)
ย ย {
ย ย ย ย tm.tv_secย =ย 0;
ย ย ย ย tm.tv_usecย =ย RETRY_TICK;
ย ย }
ย ย retย =ย select((int)sockโ€>socketย +ย 1,ย ...,ย ย &tm);
}ย whileย (retย <ย 0ย &&ย EINTRย ==ย SOCK_ERRNO);
ย ย returnย ret;
}
24
forget to check input arguments
SOCK_wait_for_ready,ย returnedย โ€1,ย errnoย 22ย :ย Invalidย arguments
25
forget to check input arguments
26
memory management
example1: buffers overflow
27
buffers overflow
strcpy(NameStr(*changeโ€>llogdataโ€>schema),ย schema_name);
strcpy(NameStr(*changeโ€>llogdataโ€>object),ย object_name);
click to see on gerrit
28
buffers overflow
use ย strncpyย instead of ย strcpyย 
strncpy(NameStr(*changeโ€>llogdataโ€>schema),
ย ย ย ย ย ย ย ย schema_name,
ย ย ย ย ย ย ย ย NAMEDATALEN);
strncpy(NameStr(*changeโ€>llogdataโ€>object),
ย ย ย ย ย ย ย ย object_name,
ย ย ย ย ย ย ย ย NAMEDATALEN);
click to see on gerrit
29
memory management
example2: free memory on stack
30
free memory on stack
char
QR_read_tuple(QResultClassย *self,ย charย binary)
{
ย ย charย  tidoidbuf[32];
ย ย ifย (field_lfย >=ย effective_cols)
ย ย ย ย bufferย =ย tidoidbuf;
ย ย else
ย ย ย ย bufferย =ย (charย *)ย malloc(lenย +ย 1);
...
this_tuplefield[field_lf].valueย =ย buffer;
...
void
QR_free_memory(QResultClassย *self)
{
ย  free(tuple[lf].value);
...
click to see on gerrit
31
memory management
example3: accessing NULL pointers
32
accessing NULL pointers
elseย if(errornumย ==ย SOCKET_CLOSED)
{
ย ย ย ย DBC_set_fullerror(self,ย 
ย ย ย ย HYT00_SOCKET_NOTEXPECT_ERROR,
ย ย ย ย sockโ€>errormsg,
ย ย ย ย "08S01");
click to see on gerrit
33
accessing NULL pointers
#defineย SOCK_get_errmsg(self)ย 
ย  (selfย ?ย selfโ€>errormsgย :ย "socketย closed")
...
elseย if(errornumย ==ย SOCKET_CLOSED)
{
ย  DBC_set_fullerror(self,
ย  HYT00_SOCKET_NOTEXPECT_ERROR,
ย  SOCK_get_errmsg(sock),
ย  "08S01");
click to see on gerrit
34
memory management
example4: memory leak
35
memory leak
HeapTuple
BuildTupleFromCStrings(AttInMetadataย *attinmeta,ย charย **values)
{
...
ย ย forย (iย =ย 0;ย iย <ย natts;ย i++)
ย ย {
ย ย ย ย ย dvalues[i]ย =
ย ย ย ย ย InputFunctionCall(&attinmetaโ€>attinfuncs[i],
ย ย ย ย ย ย ย ย ย  ย  values[i],
...
tupleย =ย heap_formtuple(tupdesc,ย dvalues,ย nulls);
...
returnย tuple;
click to see on gerrit
36
memory leak
forย (iย =ย 0;ย iย <ย natts;ย i++)
{
ย /*
ย ย *ย Freeย theย memย allocatedย inย xxx_inย toย avoidย memoryย leak
ย ย */
ย ย switch(tupdescโ€>attrs[i]โ€>atttypid)
ย ย {
ย ย /*ย Typeย belowย areย passย byย ref,ย e.g.ย seeย numeric_inย */
ย ย caseย NUMERICOID:
ย ย caseย VARCHAROID:
ย ย caseย TEXTOID:
ย ย caseย INT2VECTOROID:
ย ย caseย TIDOID:
ย ย caseย OIDVECTOROID:
ย ย ย ย ifย (NULLย !=ย dvalues[i])
ย ย ย ย {
ย ย ย ย ย ย pfree((voidย *)(dvalues[i]));
ย ย ย ย }
ย ย ย ย break;
click to see on gerrit 37
TLS - thread local storage
ODBCEnvย odbcEnvHandle;
click to see on gerrit
38
TLS - thread local storage
MT_LOCALย ODBCEnvย odbcEnvHandleย =ย NULL;
click to see on gerrit
39
compiler optimization
40
compiler optimization
UCHAR
SOCK_get_next_byte(SocketClassย *self)
{
selfโ€>buffer_filled_inย =ย recv(selfโ€>socket,ย (charย *)ย selfโ€>buffer_in,
ifย (selfโ€>buffer_filled_inย <ย 0)
{
...
SocketClassย *sc_tempย =ย self;ย /*ย rememberย theย pointerย value.ย */
readycodeย =ย SOCK_wait_for_ready(self,ย FALSE,ย retry_count);
ifย (NULLย ==ย self)
{
ย ย ifย (sc_tempย !=ย NULL)
ย ย ย ย selfย =ย sc_temp;
...
41
compiler optimization
To avoid compiler optimization:
1. we avoid to use
ifย (NULLย ==ย self)
{
ย ย ...ย codeย hereย willย beย optimizedย byย gccย โ€O2
ย ย becauseย SOCK_get_next_byteย checkย selfย !=ย NULL
ย ย ifย reachย hereย gccย thinkย selfย mustย notย beย NULL
}
2. we have to make self_value and self volatile
42
compiler optimization
volatileย voidย *ย self_value;
/*
*ย Theย belowย Cย codeย inย asmย looksย like:
*ย ย ย ย ย ย ย ย ย ย ...
*ย  ย  ย  movย ย ย ย %rbx,0x8(%rsp)ย โ€โ€saveย self
*ย  ย  ย  callqย ย 595d0ย <SOCK_wait_for_ready>
*ย  ย  ย  testย ย ย %eax,%eax
*ย  ย  ย  movย ย ย ย 0x8(%rsp),%rbxย โ€โ€restoreย self
*ย ย ย ย ย ย ย ย ย ย ...
*ย ifย youย changeย theย codeย youย haveย toย checkย theย asm
*ย thatย theย selfย isย savedย andย restored.
*/
self_valueย =ย (voidย *)self;ย /*ย rememberย theย pointerย value.ย */
readycodeย =ย SOCK_wait_for_ready(self,ย FALSE,ย retry_count);
selfย =ย (SocketClassย *)self_value;ย /*ย restoreย selfย */
click to see on gerrit
43
buggy new features/codes
44
buggy new features/codes
dump partation table
45
dump partation table
forย (cellย =ย patternsโ€>head;ย cell;ย cellย =ย cellโ€>next)
{
ย ย isparttabย =ย parsePartition(cellโ€>val,ย &maintab,ย &parttab);
ย ย /*ย bug24408ย addย _PRT_oid_ย toย parttabย */
ย ย ifย (isparttab)
ย ย ย ย partitionNameAddOid(maintab,ย &parttab);
When add new features, do not forget dump/restore, replication...
46
how to avoid bugs
think twice before you type
40% of coments/documentation.
don't ignore compiler warnings
defensive programming: do not coredump in my code!
use tools such as lint/valgrind to find out possible bugs
unit test and regression test: code coverage
47
Thanks!
Follow me on https://www.shenyu.wiki
Copyright ยฉ 2017 Yu Shen
48

More Related Content

PDF
clang-intro
PDF
100 bugs in Open Source C/C++ projects
PDF
Checking Oracle VM VirtualBox. Part 1
PPTX
The operation principles of PVS-Studio static code analyzer
PDF
Top 10 bugs in C++ open source projects, checked in 2016
PPTX
Best Bugs from Games: Fellow Programmers' Mistakes
PDF
A Check of the Open-Source Project WinSCP Developed in Embarcadero C++ Builder
PPTX
C++ Code as Seen by a Hypercritical Reviewer
clang-intro
100 bugs in Open Source C/C++ projects
Checking Oracle VM VirtualBox. Part 1
The operation principles of PVS-Studio static code analyzer
Top 10 bugs in C++ open source projects, checked in 2016
Best Bugs from Games: Fellow Programmers' Mistakes
A Check of the Open-Source Project WinSCP Developed in Embarcadero C++ Builder
C++ Code as Seen by a Hypercritical Reviewer

What's hot (20)

PPTX
C++17 now
PDF
COG Back to the Future, Part II
ย 
PPTX
Alexey Sintsov- SDLC - try me to implement
PDF
A Slipshod Check of the Visual C++ 2013 Library (update 3)
PDF
Checking the Open-Source Multi Theft Auto Game
PDF
ะ ะฐะฑะพั‚ะฐ ั ั€ะตะปัั†ะธะพะฝะฝั‹ะผะธ ะฑะฐะทะฐะผะธ ะดะฐะฝะฝั‹ั… ะฒ C++
PDF
Checking the Cross-Platform Framework Cocos2d-x
PDF
Boosting Developer Productivity with Clang
PDF
Architecture for Massively Parallel HDL Simulations
ย 
PPTX
What has to be paid attention when reviewing code of the library you develop
PDF
Picking Mushrooms after Cppcheck
PDF
One definition rule - ั‡ั‚ะพ ัั‚ะพ ั‚ะฐะบะพะต, ะธ ะบะฐะบ ั ัั‚ะธะผ ะถะธั‚ัŒ
PDF
A Post About Analyzing PHP
PDF
Kamil witecki asynchronous, yet readable, code
PDF
Windbg๋ž‘ ์นœํ•ด์ง€๊ธฐ
PPTX
ะ”ะผะธั‚ั€ะธะน ะ”ะตะผั‡ัƒะบ. ะšั€ะพััะฟะปะฐั‚ั„ะพั€ะผะตะฝะฝั‹ะน ะบั€ะฐัˆ-ั€ะตะฟะพั€ั‚
PDF
Analyzing the Dolphin-emu project
PPTX
200 Open Source Projects Later: Source Code Static Analysis Experience
PDF
Virus lab
PDF
ChakraCore: analysis of JavaScript-engine for Microsoft Edge
C++17 now
COG Back to the Future, Part II
ย 
Alexey Sintsov- SDLC - try me to implement
A Slipshod Check of the Visual C++ 2013 Library (update 3)
Checking the Open-Source Multi Theft Auto Game
ะ ะฐะฑะพั‚ะฐ ั ั€ะตะปัั†ะธะพะฝะฝั‹ะผะธ ะฑะฐะทะฐะผะธ ะดะฐะฝะฝั‹ั… ะฒ C++
Checking the Cross-Platform Framework Cocos2d-x
Boosting Developer Productivity with Clang
Architecture for Massively Parallel HDL Simulations
ย 
What has to be paid attention when reviewing code of the library you develop
Picking Mushrooms after Cppcheck
One definition rule - ั‡ั‚ะพ ัั‚ะพ ั‚ะฐะบะพะต, ะธ ะบะฐะบ ั ัั‚ะธะผ ะถะธั‚ัŒ
A Post About Analyzing PHP
Kamil witecki asynchronous, yet readable, code
Windbg๋ž‘ ์นœํ•ด์ง€๊ธฐ
ะ”ะผะธั‚ั€ะธะน ะ”ะตะผั‡ัƒะบ. ะšั€ะพััะฟะปะฐั‚ั„ะพั€ะผะตะฝะฝั‹ะน ะบั€ะฐัˆ-ั€ะตะฟะพั€ั‚
Analyzing the Dolphin-emu project
200 Open Source Projects Later: Source Code Static Analysis Experience
Virus lab
ChakraCore: analysis of JavaScript-engine for Microsoft Edge
Ad

Similar to Bug fix sharing : where does bug come from (20)

PDF
Ch 18: Source Code Auditing
PDF
CNIT 127: Ch 18: Source Code Auditing
PPTX
How to fix bug or defects in software
PPTX
Case Study of the Unexplained
PDF
100 bugs in Open Source C/C++ projects
PDF
Secure Programming With Static Analysis
PDF
Analyzing Firebird 3.0
PDF
Analyzing Firebird 3.0
PDF
Static Code Analysis and Cppcheck
PDF
An Experiment with Checking the glibc Library
PDF
Common Software Failures
PDF
Common Software Failures
PDF
Fuzzing - Part 1
PPT
Code Analysis-run time error prediction
PDF
Asterisk: PVS-Studio Takes Up Telephony
PDF
Peddle the Pedal to the Metal
ย 
PPT
Defensive programming
PDF
The Ultimate Question of Programming, Refactoring, and Everything
PDF
The Ultimate Question of Programming, Refactoring, and Everything
Ch 18: Source Code Auditing
CNIT 127: Ch 18: Source Code Auditing
How to fix bug or defects in software
Case Study of the Unexplained
100 bugs in Open Source C/C++ projects
Secure Programming With Static Analysis
Analyzing Firebird 3.0
Analyzing Firebird 3.0
Static Code Analysis and Cppcheck
An Experiment with Checking the glibc Library
Common Software Failures
Common Software Failures
Fuzzing - Part 1
Code Analysis-run time error prediction
Asterisk: PVS-Studio Takes Up Telephony
Peddle the Pedal to the Metal
ย 
Defensive programming
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and Everything
Ad

Recently uploaded (20)

PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
๐Ÿ’ฐ ๐”๐Š๐“๐ˆ ๐Š๐„๐Œ๐„๐๐€๐๐†๐€๐ ๐Š๐ˆ๐๐„๐‘๐Ÿ’๐ƒ ๐‡๐€๐‘๐ˆ ๐ˆ๐๐ˆ ๐Ÿ๐ŸŽ๐Ÿ๐Ÿ“ ๐Ÿ’ฐ
ย 
PDF
Vigrab.top โ€“ Online Tool for Downloading and Converting Social Media Videos a...
PDF
The Internet -By the Numbers, Sri Lanka Edition
ย 
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
ย 
PPTX
Digital Literacy And Online Safety on internet
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
Funds Management Learning Material for Beg
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PPTX
SAP Ariba Sourcing PPT for learning material
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
SASE Traffic Flow - ZTNA Connector-1.pdf
๐Ÿ’ฐ ๐”๐Š๐“๐ˆ ๐Š๐„๐Œ๐„๐๐€๐๐†๐€๐ ๐Š๐ˆ๐๐„๐‘๐Ÿ’๐ƒ ๐‡๐€๐‘๐ˆ ๐ˆ๐๐ˆ ๐Ÿ๐ŸŽ๐Ÿ๐Ÿ“ ๐Ÿ’ฐ
ย 
Vigrab.top โ€“ Online Tool for Downloading and Converting Social Media Videos a...
The Internet -By the Numbers, Sri Lanka Edition
ย 
Cloud-Scale Log Monitoring _ Datadog.pdf
Decoding a Decade: 10 Years of Applied CTI Discipline
Slides PPTX World Game (s) Eco Economic Epochs.pptx
522797556-Unit-2-Temperature-measurement-1-1.pptx
introduction about ICD -10 & ICD-11 ppt.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
RPKI Status Update, presented by Makito Lay at IDNOG 10
ย 
Digital Literacy And Online Safety on internet
Tenda Login Guide: Access Your Router in 5 Easy Steps
PptxGenJS_Demo_Chart_20250317130215833.pptx
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Funds Management Learning Material for Beg
Design_with_Watersergyerge45hrbgre4top (1).ppt
SAP Ariba Sourcing PPT for learning material
Module 1 - Cyber Law and Ethics 101.pptx

Bug fix sharing : where does bug come from