Download as pdf or txt
Download as pdf or txt
You are on page 1of 61

DKBA

DKBA 2826-2011.5

201159 201159


Huawei Technologies Co., Ltd.


All rights reserved
confidentiality level
DKBA 2826-2011.5

Revision declaration


DKBAxxxx.x-xxxx.xx PSST PSST
00121837 00117374
00120218
00118807
00056781 00120359
00041937
00036913 00116237
IP
00038309 00118740
00118617
00058208 00120482
00040674
00042021 00120359
00118478
00119601 00172539

00162172 00168059
49827
00162654 IP
00152313

2011-06-02 Huawei Confidential 2,61Page 2 , Total61


confidentiality level
DKBA 2826-2011.5

Table of Contents

0 ................................................................................................................................ 5
0.1 ...................................................................................................................................... 5
0.2 ....................................................................................................................... 5
0.3 .................................................................................................................... 6
0.4 ............................................................................................................................... 6
1 ........................................................................................................................................... 6
2 ............................................................................................................................................. 12
3 ....................................................................................................................... 21
3.1 ..................................................................................................................... 21
3.2 ..................................................................................................................... 23
3.3 ..................................................................................................................... 23
3.4 ..................................................................................................................... 24
3.5 ..................................................................................................................... 24
4 ............................................................................................................................................. 25
5 ...................................................................................................................................... 28
6 ...................................................................................................................................... 32
7 ...................................................................................................................................... 36
8 ............................................................................................................................................. 39
9 .................................................................................................................................. 44
10 ..................................................................................................................................... 46
11 ...................................................................................................................... 49
12 ..................................................................................................................................... 50
13 ..................................................................................................................................... 51
13.1 .................................................................................................................. 51
13.2 ............................................................................................................................. 52
13.3 .................................................................................................................. 56
13.4 I/O ........................................................................................................................ 57
13.5 .................................................................................................................................... 59
14 ................................................................................................................................. 59
15 ................................................................................................................................. 60
16 .......................................................................................................................... 60

2011-06-02 Huawei Confidential 3,61Page 3 , Total61


confidentiality level
DKBA 2826-2011.5

:
C


C
C

2011-06-02 Huawei Confidential 4,61Page 4 , Total61


confidentiality level
DKBA 2826-2011.5

0.1

1999
(what & why)
(what)

0.2

40%~90%
5100100

Harold Abelson Gerald Jay


Sussman
Steve McConnell
/
/

()
/
/50
3

2011-06-02 Huawei Confidential 5,61Page 5 , Total61


confidentiality level
DKBA 2826-2011.5

0.3

C
C

http://hi3ms.huawei.com/group/1735/threads.html
BSP

0.4

//
//

x.hy.hxyx.hy.h
y.hz.hxyz

10%
ABBCCD

.c.c
.c
.c
TDD

2011-06-02 Huawei Confidential 6,61Page 6 , Total61


confidentiality level
DKBA 2826-2011.5

google C++ Style Guide1.2


aa.haa.haa.h
aa.hbb.hbb.haa.h

1.1
ModuleUnit

.c

.c.h
CDavid R. Hanson
20041 "C Interfaces and Implementations"

1.2

.c
WORD
#include <VXWORKS.H>
#include <KERNELLIB.H>
#include <SEMLIB.H>
#include <INTLIB.H>
#include <TASKLIB.H>
#include <MSGQLIB.H>
#include <STDARG.H>
#include <FIOLIB.H>
#include <STDIO.H>
#include <STDLIB.H>
#include <CTYPE.H>
#include <STRING.H>
#include <ERRNOLIB.H>
#include <TIMERS.H>
#include <MEMLIB.H>
#include <TIME.H>
#include <WDLIB.H>
#include <SYSLIB.H>
#include <TASKHOOKLIB.H>
#include <REBOOTLIB.H>

2011-06-02 Huawei Confidential 7,61Page 7 , Total61
confidentiality level
DKBA 2826-2011.5

typedef unsigned short WORD;



WORDstdio.h syslib.h
10000100stdio.hprintf
WORDstdio.h/syslib.h9900

1.3

Robert C.Martin
20039

1.1 .c.h
.cmain

.c
.c
.c
include
ID.c
.c
void foo()
{
bar();
}

void bar()
{
Do something;
}

foobar
.cstatic
static void bar();

void foo()
{
bar();
2011-06-02 Huawei Confidential 8,61Page 8 , Total61
confidentiality level
DKBA 2826-2011.5

void bar()
{
Do something;
}

1.2
a.hb.hb.hc.hc.ha.h
a.h/b.h/c.ha.hb.hb.h
c.hc.ha.hb.h/c.h

1.3 .c/.h

god.h

1.4

a.hb.h
a.h.ca.hb.h
b.ha.h
.c/.ha.h
a.ha.ha.h

1.5 #include#define

#define FILENAME_H
PROJECTNAME_PATH_FILENAME_H
_"FILENAME_H_FILENAME_H_"_"__"
"_"

1
2

2011-06-02 Huawei Confidential 9,61Page 9 , Total61


confidentiality level
DKBA 2826-2011.5

VOStimertimer.hVOS/include/timer/timer.h,
#ifndef VOS_INCLUDE_TIMER_TIMER_H
#define VOS_INCLUDE_TIMER_TIMER_H
...
#endif

:
#ifndef TIMER_H
#define TIMER_H
..
#endif

(#ifndef XX_H)

1.6
.c

1.7 .c.cextern

a.cb.cfoo()b.hextern int foo(int input)a.c


#include <b.h>fooa.cextern int foo(int input);foo
foo

1.8 extern "C"


extern "C"extern "C"Visual Studioextern "C"

extern "C"a.hb.h

#ifndef A_H__ #ifndef B_H__


#define A_H__ #define B_H__

#ifdef __cplusplus #ifdef __cplusplus


void foo(int); extern "C" {
#define a(value) foo(value) #endif
#else
void a(int) #include "a.h"
#endif void b();

#endif /* A_H__ */ #ifdef __cplusplus


}
#endif

#endif /* B_H__ */

C++b.h
2011-06-02 Huawei Confidential 10,61Page 10 , Total61
confidentiality level
DKBA 2826-2011.5

extern "C" {
void foo(int);
void b();
}

a.hfooC++"C++"b.h#include
"a.h"extern "C" { }foo

extern C
{
#include xxx.h
...
}

#include xxx.h
extern C
{
...
}

1.1 .c
.h
.h.h

Google testGTestGTestC++1.5gtest
612gtest.hgtest.hGTest
GTestGTest
c

.h.c

VOS
vos.hVOSMemory
.c.hMemory.h

1.2 .h

1.3 .inc
.incc.inc

.inc.c
2011-06-02 Huawei Confidential 11,61Page 11 , Total61
confidentiality level
DKBA 2826-2011.5

1.1
.incsource insightVisual stduioIDE
IDE.inc
Visual Assist.h.inc

1.4

#include <a.h>
#include <b.h>
#include <c/d.h>
#include <c/e.h>
#include <f.h>
2

#include <product.h>
#include <platform.h>
product.hplatform.hproduct.h

2.1

reallocCrealloc
NULL0

NULL

char *buffer = (char *)malloc(XXX_SIZE);


.....
2011-06-02 Huawei Confidential 12,61Page 12 , Total61
confidentiality level
DKBA 2826-2011.5

buffer = (char *)realloc(buffer, NEW_SIZE);


NULLbuffer
(SRP)

2.2

UC ccb_aoc_process( )
{
... ...
struct AOC_E1_E7 aoc_e1_e7;
aoc_e1_e7.aoc = 0;
aoc_e1_e7.e[0] = 0;
... ... //aoc_e1_e7.e[i]
aoc_e1_e7.e[6] = 0;
aoc_e1_e7.tariff_rate = 0;
... ...

if (xxx)
{
if (xxx)
{
aoc_e1_e7.e[0] = 0;
... ...
aoc_e1_e7.e[6] = 0;
aoc_e1_e7.tariff_rate = 0;
}
... ...
}
else if (xxx)
{
if (xxx)
{
aoc_e1_e7.e[0] = 0;
... ...
aoc_e1_e7.e[6] = 0;

2011-06-02 Huawei Confidential 13,61Page 13 , Total61


confidentiality level
DKBA 2826-2011.5

aoc_e1_e7.tariff_rate = 0;
}
ccb_caller_e1 = aoc_e1_e7.e[0];
... ...
ccb_caller_e7 = aoc_e1_e7.e[6];
ccb_caller_tariff_rate = aoc_e1_e7.tariff_rate;
... ...
}
... ...

if (xxx)
{
if (xxx)
{
if (xxx)
{
aoc_e1_e7.e[0] = 0;
... ...
aoc_e1_e7.e[6] = 0;
aoc_e1_e7.tariff_rate = 0;
}
... ...
}
else if (xxx)
{
if (xxx)
{
aoc_e1_e7.e[0] = 0;
... ...
aoc_e1_e7.e[6] = 0;
aoc_e1_e7.tariff_rate = 0;
}
ccb_caller_e1 = aoc_e1_e7.e[0];
... ...
ccb_caller_e7 = aoc_e1_e7.e[6];
ccb_caller_tariff_rate = aoc_e1_e7.tariff_rate;
... ...
}
return 1;
}
else
{
return 0;
2011-06-02 Huawei Confidential 14,61Page 14 , Total61
confidentiality level
DKBA 2826-2011.5

}
}

2.1 50

2.1
NBNC[150]
50

Logiscope"Number of Statement"
20QA C50

2.2 4

ifforwhileswitch

[1, 4]
5
void serial (void)
{
if (!Received)
{
TmoCount = 0;
switch (Buff)
{
case AISGFLG:
if ((TiBuff.Count > 3)
&& ((TiBuff.Buff[0] == 0xff) || (TiBuf.Buff[0] == CurPa.ADDR)))
{
Flg7E = false;
Received = true;
}
else
{
TiBuff.Count = 0;
Flg7D = false;
Flg7E = true;
}
break;
default:
break;
}
}
}
2011-06-02 Huawei Confidential 15,61Page 15 , Total61
confidentiality level
DKBA 2826-2011.5

2.3

static
Cstatic

square_examg_exam
int g_exam;
unsigned int example( int para )
{
unsigned int temp;

g_exam = para; // **
temp = square_exam ( );

return temp;
}
**
g_exam
paratemp =square_exam ( )temp

int g_exam;
unsigned int example( int para )
{
unsigned int temp;

[] //
g_exam = para; //g_exam
temp = square_exam( ); //
[] //
//
return temp;
}

2.4 /

void PidMsgProc(MsgBlock *Msg)


{
MsgProcItem *func = NULL;
2011-06-02 Huawei Confidential 16,61Page 16 , Total61
confidentiality level
DKBA 2826-2011.5

if (Msg == NULL)
{
return;
}
... ...
GetMsgProcFun(Msg, &func);

func(Msg);

return;
}

int GetMsgProcFun(MsgBlock *Msg, MsgProcItem **func)


{
if (Msg == NULL)
{
return 1;
}

... ...
*func = VOS_NULL_PTR;

for (Index = 0; Index < NELEM(g_MsgProcTable); Index++)


{
if ((g_MsgProcTable[Index].FlowType == Msg->FlowType)
&& (g_MsgProcTable[Index].Status == Msg->Status)
&& (g_MsgProcTable[Index].MsgType == Msg->MsgType))
{
*func = &(g_MsgProcTable[Index]);
return 0;
}
}

return 1;
}

int ServiceProcess(int CbNo, MsgBlock *Msg)


{
if ( Msg == NULL)
{
return 1;
}

... ...
//
... ...

return 0;
}

2.5
2011-06-02 Huawei Confidential 17,61Page 17 , Total61
confidentiality level
DKBA 2826-2011.5

//

FILE *fp = fopen( "./writeAlarmLastTime.log","r");


if(fp == NULL)
{
return;
}
char buff[128] = "";
fscanf(fp,%s, buff); /* writeAlarmLastTime.log
buff */
fclose(fp);
long fileTime = getAlarmTime(buff); /* getAlarmTime
buff */

FILE *fp = fopen( "./writeAlarmLastTime.log","r");


if(fp == NULL)
{
return;
}
char buff[128] = "";
if (fscanf(fp,%s,buff) == EOF) //fscanf
{
fclose(fp);
return;
}
fclose(fp);
long fileTime = getAlarmTime(buff); //

2.6 7

3~5
2011-06-02 Huawei Confidential 18,61Page 18 , Total61
confidentiality level
DKBA 2826-2011.5

Fan-inFan-outHenryKafura1981
coupling//
The Fan-in (Informational fan-in) metric measures the fan-in of a module. The fan-in of a
module A is the number of modules that pass control into module A.
The Fan-out metric measures the number of the number of modules that are called by a given
module.

2.7 )

2.1 const
/const
/
C99 7.21.4.4 strncmp const
int strncmp(const char *s1, const char *s2, register size_t n)
{
register unsigned char u1, u2;

while (n-- > 0)


{
u1 = (unsigned char) *s1++;
u2 = (unsigned char) *s2++;

if (u1 != u2)
{
return u1 - u2;
}

if (u1 == '\0')
{
return 0;
}
}

return 0;
}
pc-lint 8.0pc-lint.pdf11.4 const Checking

2011-06-02 Huawei Confidential 19,61Page 19 , Total61


confidentiality level
DKBA 2826-2011.5

2.2 I/O

Cstatic

staticauto

unsigned int integer_sum( unsigned int base )


{
unsigned int index;
static unsigned int sum = 0;// static
// auto
for (index = 1; index <= base; index++)
{
sum += index;
}

return sum;
}
erlangdirty

2.3

hr = root_node->get_first_child(&log_item); // list.xml log_item


..
hr = log_item->get_next_sibling(&media_next_node); // log_item

hr = root_node->get_first_child(&log_item);
..
if (log_item == NULL) //
{
return retValue;
}
hr = log_item->get_next_sibling(&media_next_node);

2.4 5

2.5

2011-06-02 Huawei Confidential 20,61Page 20 , Total61


confidentiality level
DKBA 2826-2011.5

2.6 static
staticstatic

STATICSTATICstatic

#ifdef _DEBUG
#define STATIC static
#else
#define STATIC
#endif

3.1

unix like_text_mutex
kernel_text_address
WindowsWindows
RFCReadRFCTextunix
likeread_rfc_text

2080
win32APIMFC
.NET Framework.NET Framework

Java(
http://zh.wikipedia.org/wiki/%E5%8C%88%E7%89%99%E5%88%A9%E5%91%BD%E5%90%8D%E6%B3%95)
http://en.wikipedia.org/wiki/Hungarian_notation
unixwindows

3.1

2011-06-02 Huawei Confidential 21,61Page 21 , Total61


confidentiality level
DKBA 2826-2011.5

int error_number;
int number_of_completed_connection;

int n;
int nerr;
int n_comp_conns;

3.2

argument arg
buffer buff
clock clk
command cmd
compare cmp
configuration cfg
device dev
error err
hexadecimal hex
increment inc
initialize init
maximum max
message msg
minimum min
parameter para
previous prev
register reg
semaphore sem
statistic stat
synchronize sync
temp tmp

3.1 /
Unix likewindows like

3.1

add/remove begin/end create/destroy


2011-06-02 Huawei Confidential 22,61Page 22 , Total61
confidentiality level
DKBA 2826-2011.5

insert/delete first/last get/release


increment/decrement put/get add/delete
lock/unlock open/close min/max
old/new start/stop next/previous
source/target show/hide send/receive
source/destination copy/paste up/down

3.2

#define EXAMPLE_0_TEST_
#define EXAMPLE_1_TEST_

#define EXAMPLE_UNIT_TEST_
#define EXAMPLE_ASSERT_TEST_

3.3



foo.cabfoo.c
a_module_foo.cb_module_foo.c

3.4 //

3.5 /

3.2

3.6
MSDOSWindowsLinux

3.3

3.2 g_
3.3 s_

2011-06-02 Huawei Confidential 23,61Page 23 , Total61


confidentiality level
DKBA 2826-2011.5

g_s_

g_s_

3.4 ijk

3.7

3.8

3.4

3.9

DWORD GetCurrentDirectory( DWORD BufferLength, LPTSTR Buffer );

3.10

3.5

3.5 _

#define PI_ROUNDED 3.14

3.6 _
_ISO/IEC 9899C99
6.10.8 Predefined macro names
None of these macro names, nor the identifier
defined, shall be the subject of a #define or a #undef preprocessing directive.
Any other predefined macro names shall begin with a leading underscore followed
by an uppercase letter or a second underscore.

2011-06-02 Huawei Confidential 24,61Page 24 , Total61


confidentiality level
DKBA 2826-2011.5

2Steve McConnell ///


20063"11"

4
4.1

WORD DelRelTimeQue(void)
{
WORD Locate;

Locate = 3;
Locate = DeleteFromQue(Locate); /* LocateDeleteFromQue
*/
return Locate;
}

WORD DelRelTimeQue(void)
{
WORD Ret;
WORD Locate;

Locate = 3;
Ret = DeleteFromQue(Locate);

return Ret;
}

4.2

typedef struct STUDENT_STRU


{
unsigned char name[32]; /* student's name */
unsigned char age; /* student's age */
unsigned char sex; /* student's sex, as follows */

2011-06-02 Huawei Confidential 25,61Page 25 , Total61


confidentiality level
DKBA 2826-2011.5

/* 0 - FEMALE; 1 - MALE */
unsigned char teacher_name[32]; /* the student teacher's name */
unsigned char teacher_sex; /* his teacher sex */
} STUDENT;

typedef struct TEACHER_STRU


{
unsigned char name[32]; /* teacher name */
unsigned char sex; /* teacher sex, as follows */
/* 0 - FEMALE; 1 - MALE */
unsigned int teacher_ind; /* teacher index */
} TEACHER;

typedef struct STUDENT_STRU


{
unsigned char name[32]; /* student's name */
unsigned char age; /* student's age */
unsigned char sex; /* student's sex, as follows */
/* 0 - FEMALE; 1 - MALE */
unsigned int teacher_ind; /* his teacher index */
} STUDENT;

4.3
static
static
STATICSTATICstatic

#ifdef _DEBUG
#define STATIC static
#else
#define STATIC
#endif

4.1

4.2
cpu32/64

2011-06-02 Huawei Confidential 26,61Page 26 , Total61


confidentiality level
DKBA 2826-2011.5

4.3
4.3

4.1

4.2 API

SetFanWorkModeGetFanWorkMode
GetFanSpeedEnabletFanSavePower

4.3
CC++

//
int speedup_factor 0;
if (condition)
{
speedup_factor = 2;
}
else
{
speedup_factor = -1;
}

//
int speedup_factor;
if (condition)
{
speedup_factor = 2;
}
else
{
speedup_factor = -1;
}

//

2011-06-02 Huawei Confidential 27,61Page 27 , Total61


confidentiality level
DKBA 2826-2011.5

int speedup_factor = -1;


if (condition)
{
speedup_factor = 2;
}

//?:
int speedup_factor = condition?2:-1;

//
int speedup_factor = ComputeSpeedupFactor()

4.4

4.5

char ch;
unsigned short int exam;

ch = -1;
exam = ch; // exam0xFFFF

5.1

#define RECTANGLE_AREA(a, b) a * b
#define RECTANGLE_AREA(a, b) (a * b)
#define RECTANGLE_AREA(a, b) (a) * (b)

#define RECTANGLE_AREA(a, b) ((a) * (b))

#define RECTANGLE_AREA(a, b) a * b #define RECTANGLE_AREA(a, b) (a * b)


c/RECTANGLE_AREA(a, b) c/a * b , c b

#define RECTANGLE_AREA(a, b) (a) * (b)
2011-06-02 Huawei Confidential 28,61Page 28 , Total61
confidentiality level
DKBA 2826-2011.5

RECTANGLE_AREA(c + d, e + f)(c + d * e + f), de

5.2
do while(0)

#define FOO(x) \
printf("arg is %d\n", x); \
do_something_useful(x);

for
for (blah = 1; blah < 10; blah++)
FOO(blah)

#define FOO(x) { \
printf("arg is %s\n", x); \
do_something_useful(x); \
}

if (condition == 1)
FOO(10);
else
FOO(20);

#define FOO(x) do { \
printf("arg is %s\n", x); \
do_something_useful(x); \
} while(0)

do-while(0)

5.3

#define SQUARE(a) ((a) * (a))


int a = 5;
int b;
b = SQUARE(a++); // a = 7

b = SQUARE(a);
a++; // a = 6

2011-06-02 Huawei Confidential 29,61Page 29 , Total61


confidentiality level
DKBA 2826-2011.5

5.4

const

const//
0

5.1

#define SQUARE(a) (a) * (a)


SQUARE(i++)idouble square(double a) {return a * a;}

#define MAX_MACRO(a, b) ((a) > (b) ? (a) : (b))


int MAX_FUNC(int a, int b) {
return ((a) > (b) ? (a) : (b));
}
int testFunc()
{
unsigned int a = 1;
int b = -1;
printf("MACRO: max of a and b is: %d\n", MAX_MACRO(++a, b));
printf("FUNC : max of a and b is: %d\n", MAX_FUNC(a, b));
return 0;
}

(a < b)a
MACRO: max of a and b is: -1
FUNC : max of a and b is: 2

5.2 const
#define

#define ASPECT_RATIO 1.653

2011-06-02 Huawei Confidential 30,61Page 30 , Total61


confidentiality level
DKBA 2826-2011.5

ASPECT_RATIO
ASPECT_RATIO
1.653ASPECT_RATIOASPECT_RATIO
1.653

const double ASPECT_RATIO = 1.653;

const
constchar*const
const char * const authorName = "Scott Meyers";
constISO/IEC 9899C99
The following pair of declarations demonstrates the difference between a "variable pointer
to a constant value" and a "constant pointer to a variable value".
const int *ptr_to_constant;
int *const constant_ptr;
The contents of any object pointed to by ptr_to_constant shall not be modified through that
pointer,but ptr_to_constant itself may be changed to point to another object. Similarly, the
contents of the intpointed to by constant_ptrmay be modified, but constant_ptritself shall
always point to the same location.

The declaration of the constant pointer constant_ptr may be clarified by including a


definition for the type "pointer to int".
typedef int *int_ptr;
const int_ptr constant_ptr;
declares constant_ptras an object that has type "const-qualified pointer to int".

5.3 returngotocontinuebreak

CHECK_AND_RETURN
#define CHECK_AND_RETURN(cond, ret) {if (cond == NULL_PTR) {return ret;}}
():
pMem1 = VOS_MemAlloc(...);
CHECK_AND_RETURN(pMem1 , ERR_CODE_XXX)
pMem2 = VOS_MemAlloc(...);
CHECK_AND_RETURN(pMem2 , ERR_CODE_XXX) /*pMem2==NULL_PTRpMem1
*/

CHECK_AND_RETURN

2011-06-02 Huawei Confidential 31,61Page 31 , Total61


confidentiality level
DKBA 2826-2011.5

6
6.1
1
2
3
4
5

6
7
8/

6.2

1
C
= ==
< "<<"
> >>
|| "|"
&& "&"
"!" ~

2
(1) "/"
/
:1/2=0
(2)"%"
"%"
5%2=15.0%2
(3)++--
1
k = 5;
x = k++;
x = 5k = 6
2
k = 5;
x = ++k;
x = 6k = 6
3

2011-06-02 Huawei Confidential 32,61Page 32 , Total61


confidentiality level
DKBA 2826-2011.5

k = 5;
x = k--;
x = 5k = 4
4
k = 5;
x = --k;
x = 4k = 4

6.3

6.4

http://local.joelonsoftware.com/mediawiki/index.php?title=Chinese_%28Simplified%29&o
ldid=9699

6.1

itoa
char TempShold[10] ;
itoa(ProcFrecy,TempShold, 10); /* 1073741823coredump,
*/
TempShold\09ProcFrecy10
TempShold
int3221474836472147483648TempShold12
char TempShold[12] ;
itoa(ProcFrecy,TempShold,10);


sprintf /vsprintf/strcpy/strcat/gets
snprintf/strncpy/strncat/fgets
memcpy/memset
\0 \0


sizeofstrlen/
.ppt

2011-06-02 Huawei Confidential 33,61Page 33 , Total61


confidentiality level
DKBA 2826-2011.5

6.2
//Socket///GUI

MsgDBDEV = (PDBDevMsg)GetBuff( sizeof( DBDevMsg ), __LINE__);


if (MsgDBDEV == NULL)
{
return;
}
MsgDBAppToLogic = (LPDBSelfMsg)GetBuff( sizeof(DBSelfMsg), __LINE__ );
if ( MsgDBAppToLogic == NULL )
{
return; //MsgDB_DEV
}

//Socket///GUI



returnbreak

.ppt

6.3

int* foobar (void)


{
int local_auto = 100;
return &local_auto;
}

NULL



.ppt

6.4 1
<=<>=>

2011-06-02 Huawei Confidential 34,61Page 34 , Total61


confidentiality level
DKBA 2826-2011.5

C-128127
char ch = 127;
int sum = 200;
ch += 1; // 127chch-128128
sum += ch; // sum32872

6.5 if ... else ifelse switchdefault

6.1

6.2 ifelseelse

6.3 goto
gotogoto
goto

goto

int foo(void)
{
char* p1 = NULL;
char* p2 = NULL;
char* p3 = NULL;
int result = -1;

p1 = (char *)malloc(0x100);
if (p1 == NULL)
{
goto Exit0;
}
strcpy(p1, "this is p1");

p2 = (char *)malloc(0x100);
if (p2 == NULL)
{
goto Exit0;
}
strcpy(p2, "this is p2");

p3 = (char *)malloc(0x100);
if (p3 == NULL)
{
goto Exit0;
}
strcpy(p3, "this is p3");
2011-06-02 Huawei Confidential 35,61Page 35 , Total61
confidentiality level
DKBA 2826-2011.5

result = 0;

Exit0:
free(p1); // Cfree
free(p2);
free(p3);

return result;
}

6.4

unsigned char size ;



while (size-- >= 0) //
{
... // program code
}
size000xFF
char size; // unsigned char char

while (size-- >= 0)
{
... // program code
}

7
7.1

int foo()
{
if ()
{
;
return ERR_CODE_1;
}
if (
{
;
return ERR_CODE_2;
2011-06-02 Huawei Confidential 36,61Page 36 , Total61
confidentiality level
DKBA 2826-2011.5

}
;
return SUCCESS;
}
if else

int foo()
{
if ()
{
;
return SUCCESS;
}
else if ()
{
;
return ERR_CODE_1;
}
else
{
;
return ERR_CODE_2;
}
}
foo

7.2

7.1

for (int i = 0; i < 10; i++ )


{
sum += i;
back_sum = sum;
}
forback_Sum = sum;
for (int i = 0; i < 10; i++ )
{
sum += i;
}
2011-06-02 Huawei Confidential 37,61Page 37 , Total61
confidentiality level
DKBA 2826-2011.5

back_sum = sum;

for (_UL i = 0; i < func_calc_max(); i++)


{
//process;
}
func_calc_max()
_UL max = func_calc_max();
for (_UL i = 0; i < max; i++)
{
//process;
}

7.2
SIZE_B

cachecacheij
...
for (int i = 0; i < SIZE_B; i++)
{
for (int j = 0; j < SIZE_A; j++)
{
sum += x[j][i];
}
}
...

SIZE_B :
...
for (int i = 0; i < SIZE_B; i++)
{
for (int j = 0; j < SIZE_A; j++)
{
sum += x[i][j];
}
}
...

7.3

2011-06-02 Huawei Confidential 38,61Page 38 , Total61


confidentiality level
DKBA 2826-2011.5

7.4 inline
inlineinline
inlineSIZE

8
8.1

/* m*/
/* : : */
int p(int m)
{
int k = sqrt(m);
for (int i = 2; i <= k; i++)
if (m % i == 0)
break; /* m*/
/* */
if (i > k)
return 1;
/* */
else
return 0;
}

int IsPrimeNumber(int num)


{
int sqrt_of_num = sqrt (num);
for (int i = 2; i <= sqrt_of_num; i++)
{
if (num % i == 0)
{
return FALSE;
}
}
return TRUE;
}

8.2

2011-06-02 Huawei Confidential 39,61Page 39 , Total61


confidentiality level
DKBA 2826-2011.5

/* IDID*/
/* ID */
/* IDClearIdAlarmId */
if (CLEAR_ALARM_LEVEL != RcData.level)
{
SetAlarmID(RcData.AlarmId);
}
else
{
SetAlarmID(RcData.ClearId);
}

/* IDIDIDClearId
AlarmId*/

8.3

whatwhy

++i; /* increment i */
if (receive_flag) /* if receive_flag is TRUE */

/* :04*/

/* xxxx
-
*/
int time = 0;
do
{
write_reg(some_addr, value);
time++;
} while ((read_reg(some_addr) != value) && (time < 3));

/* Divide result by two, taking into account that x contains the carry from the add. */
for (int i = 0; i < result->size(); i++)
{
x = (x << 8) + (*result)[i];
(*result)[i] = x >> 1;
x &= 1;
2011-06-02 Huawei Confidential 40,61Page 40 , Total61
confidentiality level
DKBA 2826-2011.5

8.1

8.2

Copyright Huawei Technologies Co., Ltd. 1998-2011. All rights reserved.


1998-2011 19982011

/*************************************************
Copyright Huawei Technologies Co., Ltd. 1998-2011. All rights reserved.
File name: //
Author: ID Version: Date: //
Description: //
//
//
Others: //
History: //
//
1. Date:
Author: ID:
Modification:
2. ...
*************************************************/

8.3

8.4

/* The ErrorCode when SCCP translate */


/* Global Title failure, as follows */ /* */
/* 0 SUCCESS 1 GT Table error */
/* 2 GT error Others no use */ /* */
/* only function SCCPTranslate() in */
/* this modual can modify it, and other */
/* module can visit it through call */
2011-06-02 Huawei Confidential 41,61Page 41 , Total61
confidentiality level
DKBA 2826-2011.5

/* the function GetGTTransErrorCode() */ /* */


BYTE g_GTTranErrorCode;

8.5

/* active statistic task number */


#define MAX_ACT_TASK_NUMBER 1000

#define MAX_ACT_TASK_NUMBER 1000 /* active statistic task number */


//
/* sccp interface with sccp user primitive message name */
enum SCCP_USER_PRIMITIVE
{
N_UNITDATA_IND, /* sccp notify sccp user unit data come */
N_NOTICE_IND, /* sccp notify user the No.7 network can not transmission this message */
N_UNITDATA_REQ, /* sccp user's unit data transmission request*/
};

8.6 switchcasecasecase
casecase
break

case CMD_FWD:
ProcessFwd();
/* now jump into case CMD_A */
case CMD_A:
ProcessA();
break;
//case
switch (cmd_flag)
{
case CMD_A:
case CMD_B:
{
ProcessCMD();
break;
}

}

8.7

8.8

2011-06-02 Huawei Confidential 42,61Page 42 , Total61


confidentiality level
DKBA 2826-2011.5

8.1

8.2

8.3
doxygen
doxygen

/**
* @file egmib.h
* @brief
* @version 1.1
* @author eg
* @date eg20101215
*/

/**
*@ Description:SET
* @param req - SNMP SET .
* @param ind - subrequest .
* @return SNMP_ERROR_SUCCESSSNMP_ERROR_COMITFAIL
*/

Int commit_set_request(Request *req, int ind);

/** Agent MIB */


agentpp_simulation_mib * g_agtSimMib;

12Steve McConnell /// 20063


"32"
2Robert C.Martin 20101"
"
3Robert C.Martin 2003
9"5"
4Doxygenhttp://hi3ms.huawei.com/group/1735/files.html
2011-06-02 Huawei Confidential 43,61Page 43 , Total61
confidentiality level
DKBA 2826-2011.5

9
9.1 4
/IDETAB
/IDETAB
IDE
/

9.2

if (!valid_ni(ni))
{
// program code
...
}
repssn_ind = ssn_data[index].repssn_index;
repssn_ni = ssn_data[index].ni;

if (!valid_ni(ni))
{
// program code
...
}

repssn_ind = ssn_data[index].repssn_index;
repssn_ni = ssn_data[index].ni;

9.3

PC13280/132VTYPC

if ((temp_flag_var == TEST_FLAG)
&&(((temp_counter_var - TEST_COUNT_BEGIN) % TEST_COUNT_MODULE) >= TEST_COUNT_THRESHOLD))
{
// process code
}

2011-06-02 Huawei Confidential 44,61Page 44 , Total61


confidentiality level
DKBA 2826-2011.5

9.4

int a = 5; int b= 10; //

int a = 5;
int b= 10;

9.5 iffordowhilecaseswitchdefault
iffordowhilecaseswitchdefault

iffordowhile{}
iffordowhile{}
if/else{}{}
{ifif
if{

9.6
>

()
C

(1)
int a, b, c;
(2) , "=" "+=""+""%""&&""&"
"<<""^"
if (current_time >= MAX_TIME_VALUE)
a = b + c;
a *= 2;
a = b ^ 2;
(3) "!""~""++""--""&"
*p = 'a'; // "*"
flag = !is_empty; // "!"
p = &mem; // "&"
i++; // "++","--"
(4) "->""."
p->id = pid; // "->"
2011-06-02 Huawei Confidential 45,61Page 45 , Total61
confidentiality level
DKBA 2826-2011.5

(5) ifforwhileswitchif
if (a >= b && c > d)

9.1 /*//*/

'//'

9.2

10

10.1
( )&&| |? : ,

x = b[i] + i++;
b[i] i ++

x = b[i] + i;
i ++;
2

x = func( i++, i);

i++;
x = func(i, i);
3

p->task_start_fn(p++);
pp++p++
p->task_start_fn(p);
p++;
4

int g_var = 0;
2011-06-02 Huawei Confidential 46,61Page 46 , Total61
confidentiality level
DKBA 2826-2011.5

int fun1()
{
g_var += 10;
return g_var;
}

int fun2()
{
g_var += 100;
return g_var;
}
int x = fun1() + fun2();

fun1()fun2()xfun1()/fun2()
fun1()/fun2()
fun1/ fun2
int x = fun1();
x = x + fun2();

x = y = y = z / 3;
x = y = y++;

6volatile
volatile
volatile

/* volumevolatile*/
UINT16 x = ( volume << 3 ) | volume; /* volume

*/

10.1

int g_var;
int fun1()
{
g_var += 10;
return g_var;
}

int fun2()
{
2011-06-02 Huawei Confidential 47,61Page 47 , Total61
confidentiality level
DKBA 2826-2011.5

g_var += 100;
return g_var;
}
int main(int argc, char *argv[], char *envp[])
{
g_var = 1;
printf("func1: %d, func2: %d\n", fun1(), fun2());

g_var = 1;
printf("func2: %d, func1: %d\n", fun2(), fun1());
}

10.2 if
if

int main(int argc, char *argv[], char *envp[])


{
int a = 0;
int b;
if ((a == 0) || ((b = fun1()) > 10))
{
printf("a: %d\n", a);
}
printf("b: %d\n", b);
}

int g_var;
int main(int argc, char *argv[], char *envp[])
{
g_var = 1;
printf("set 1st: %d, add 2nd: %d\n", g_var = 10, g_var++);
g_var = 1;
printf("add 1st: %d, set 2nd: %d\n", g_var++, g_var = 10);
}

10.3

1.
x = ~a; /* */
x = -a; /* */
2.
x = a + b + c; /* */
2011-06-02 Huawei Confidential 48,61Page 48 , Total61
confidentiality level
DKBA 2826-2011.5

x = f ( a + b, c ) /* */
if (a && b && c) /* */
x = (a * 3) + c + d; /* */
x = ( a == b ) ? a : ( a b ); /* */
3 .
3
/* (,)(&&)(||)C
f4 = (f1 + f2) + f3 f4 = f1 + (f2 + f3)
f4
*/
f4 = f1 + f2 + f3;
.
10.4
=
= =

x = y;
if (x != 0)
{
foo ();
}

if (( x = y ) != 0)
{
foo ();
}

if (x = y)
{
foo ();
}

11
11.1

11.2

11.3 PC-Lint

2011-06-02 Huawei Confidential 49,61Page 49 , Total61


confidentiality level
DKBA 2826-2011.5

11.4

11.1

12
12.1

12.1

12.2

12.3

12.4

2011-06-02 Huawei Confidential 50,61Page 50 , Total61


confidentiality level
DKBA 2826-2011.5

12.1

13

13.1





sql
DOS
SQL

13.1

13.1 NULL
C\0NULLstrcpy()strlen()
NULLNULL

strncpy()strcpy()
strncat()strcat()
snprintf()sprintf()
fgets()gets()
NULL
nNULLNULL

char a[16];
2011-06-02 Huawei Confidential 51,61Page 51 , Total61
confidentiality level
DKBA 2826-2011.5

strncpy(a, "0123456789abcdef", sizeof(a));


strncpy()aNULL
NULL
NULL
char a[16];
strncpy(a, "0123456789abcdef", sizeof(a) - 1 );
a[sizeof(a) - 1] = '\0';

13.2
gets()getenv()scanf()

char buff[256];
char *editor = getenv("EDITOR");
if (editor != NULL)
{
strcpy(buff, editor);
}
"EDITOR"buff

malloc
char *buff;
char *editor = getenv("EDITOR");
if (editor != NULL)
{
buff = malloc(strlen(editor) + 1);
if (buff != NULL)
{
strcpy(buff, editor);
}
}

13.2

C99integer promotionsinteger conversion rank


usual arithmetic conversions

13.3

1
int i;
unsigned int j;

i = INT_MAX; // 2,147,483,647
i++;
2011-06-02 Huawei Confidential 52,61Page 52 , Total61
confidentiality level
DKBA 2826-2011.5

printf("i = %d\n", i); // i=-2,147,483,648

j = UINT_MAX; // 4,294,967,295;
j++;
printf("j = %u\n", j); // j = 0

i = INT_MIN; // -2,147,483,648;
i--;
printf("i = %d\n", i); // i = 2,147,483,647

j = 0;
j--;
printf("j = %u\n", j); // j = 4,294,967,295

2
/* FSM*/
unsigned int length;

length -= FSM_HDRLEN ;
lengthFSM_HDRLENlength

if (length < FSM_HDRLEN )


{
return VOS_ERROR;
}
length -= FSM_HDRLEN ;

13.4

high-order bit

#define BUF_SIZE 10
int main(int argc,char* argv[])
{
int length;
char buf[BUF_SIZE];

if (argc != 3)
{
return -1;
}
length = atoi(argv[1]); //atoi

2011-06-02 Huawei Confidential 53,61Page 53 , Total61


confidentiality level
DKBA 2826-2011.5

if (length < BUF_SIZE) // len


{
memcpy(buf, argv[2], length); /* lensize_t
memcpy()buf */
printf("Data copied\n");
}
else
{
printf("Too many data\n");
}
}

1len
#define BUF_SIZE 10

int main(int argc, char* argv[])


{
unsigned int length;
char buf[BUF_SIZE];

if (argc != 3)
{
return -1;
}

length = atoi(argv[1]);

if (length < BUF_SIZE)


{
memcpy(buf, argv[2], length);
printf("Data copied\n");
}
else
{
printf("Too much data\n");
}
return 0;
}
2len
#define BUF_SIZE 10

int main(int argc, char* argv[])


{
int length;
char buf[BUF_SIZE];

if (argc != 3)
{
return -1;
}

2011-06-02 Huawei Confidential 54,61Page 54 , Total61


confidentiality level
DKBA 2826-2011.5

length = atoi(argv[1]);

if ((length > 0) && (length < BUF_SIZE))


{
memcpy(buf, argv[2], length);
printf("Data copied\n");
}
else
{
printf("Too much data\n");
}
return 0;
}

13.5

int main(int argc, char* argv[])


{
unsigned short total = strlen(argv[1]) + strlen(argv[2]) + 1;
char* buffer = (char*)malloc(total);

strcpy(buffer, argv[1]);
strcat(buffer, argv[2]);
free(buffer);

return 0;
}
totalunsigned shortstrlen()size_tunsigned
long6550036unsigned long65500+36+1
total65500+36+1%65536 = 1malloc()buff1strcpy()
strcat()

int main(int argc, char* argv[])


{
size_t total = strlen(argv[1]) + strlen(argv[2]) + 1;
if ((total <= strlen(argv[1])) || (total <= strlen(argv[2])))
{
/* handle error */
return -1;
}

char* buffer = (char*)malloc(total);


strcpy(buffer, argv[1]);
strcat(buffer, argv[2]);
free(buffer);

2011-06-02 Huawei Confidential 55,61Page 55 , Total61


confidentiality level
DKBA 2826-2011.5

return 0;
}

13.3

13.6

char *error_msg = "Resource not available to user.";


int error_type = 3;
/* */
printf("Error (type %s): %d\n", error_type, error_msg);

/* */
printf("Error: %s\n");

copy-paste

13.7
I/O

1
char input[1000];
if (fgets(input, sizeof(input) - 1, stdin) == NULL)
{
/* handle error */
}
input[sizeof(input)-1] = '\0';
printf(input);

inputprintf()
%s%s%s%s%s%s%s%s%s%s%s%s%s
inputprintf()

printf()%s
input
char input[1000];
if (fgets(input, sizeof(input)-1, stdin) == NULL)
{
/* handle error */
}
input[sizeof(input)-1] = '\0';
printf(%s, input);

2011-06-02 Huawei Confidential 56,61Page 56 , Total61


confidentiality level
DKBA 2826-2011.5

2
void check_password(char *user, char *password)
{
if (strcmp(password(user), password) != 0)
{
char *msg = malloc(strlen(user) + 100);
if (!msg)
{
/* handle error condition */
}
sprintf(msg, "%s login incorrect", user);
fprintf(STDERR, msg);
syslog(LOG_INFO, msg);
free(msg);
}
/**/
}

user %s%s%s%s%s%s%s%s%s%s%s%ssprintf()msg
%s%s%s%s%s%s%s%s%s%s%s%s login incorrectfprintf()msg
fprintf()1syslog()

void check_password(char *user, char *password)


{
if (strcmp(password(user), password) != 0)
{
char *msg = malloc(strlen(user) + 100);
if (!msg)
{
/* handle error condition */
}
sprintf(msg, "%s password incorrect", user);
fprintf(stderr, "%s", user);
syslog(LOG_INFO, "%s", msg);
free(msg);
}
}

13.4 I/O

13.8 strlen()
strlen()NULL
strlen()I/O

char buf[BUF_SIZE + 1];

2011-06-02 Huawei Confidential 57,61Page 57 , Total61


confidentiality level
DKBA 2826-2011.5

if (fgets(buf, sizeof(buf), fp) == NULL)


{
/* handle error */
}
buf[strlen(buf) - 1] = '\0';
\nbufNULLstrlen(buf)
0buf[-1]
NULL
char buf[BUF_SIZE + 1];
char *p;
if (fgets(buf, sizeof(buf), fp))
{
p = strchr(buf, '\n');
if (p)
{
*p = '\0';
}
}
else
{
/* handle error condition */
}

13.9 intI/O
I/Ofgetc()getc()getchar()int
EOFfputc()putc()putchar()ungetc()
EOF
I/OEOFcharchar8
int32getchar()ASCII0xFFchar
EOF0xFFFFFFFFEOF

char buf[BUF_SIZE];
char ch;
int i = 0;
while ( (ch = getchar()) != '\n' && ch != EOF )
{
if ( i < BUF_SIZE - 1 )
{
buf[i++] = ch;
}
}
buf[i] = '\0'; /* terminate NTBS */
intgetchar()
char buf[BUF_SIZE];
int ch;
int i = 0;
while (((ch = getchar()) != '\n') && ch != EOF)
{
if (i < BUF_SIZE - 1)
2011-06-02 Huawei Confidential 58,61Page 58 , Total61
confidentiality level
DKBA 2826-2011.5

{
buf[i++] = ch;
}
}
buf[i] = '\0'; /* terminate NTBS */
sizeof(int) == sizeof(char)intEOFfeof()
ferror()

13.5

13.10
C99system()UNIXshellWindowsCMD.exe
/POSIXpopen()
system()system()

system(sprintf("any_exe %s", input));

happy; useradd attacker


shellany_exe happy; useradd attacker
POSIXexecve()system().
void secuExec (char *input)
{
pid_t pid;
char *const args[] = {"", input, NULL};
char *const envs[] = {NULL};
pid = fork();
if (pid == -1)
{
puts("fork error");
}
else if (pid == 0)
{
if (execve("/usr/bin/any_exe", args, envs) == -1)
{
puts("Error executing any_exe");
}
}
return;
}
Windowsexecve()Win32 API CreateProcess()system()

14
14.1 /

2011-06-02 Huawei Confidential 59,61Page 59 , Total61


confidentiality level
DKBA 2826-2011.5

14.1

15
15.1 /

15.1

DOSBC3.1
void main()
{
register int index; //

_AX = 0x4000; // _AXBC3.1


... // program code
}

15.2

16

google C++

C++

C++

C++

2011-06-02 Huawei Confidential 60,61Page 60 , Total61


confidentiality level
DKBA 2826-2011.5

C(MISRA)
CC
C
C

CC

2011-06-02 Huawei Confidential 61,61Page 61 , Total61

You might also like