SlideShare ist ein Scribd-Unternehmen logo
1 von 56
SafeInt

안전한 정수 연산을 향해서…
jiniya.net
int a, b, c;
int d = a * b + c;
short len;
len = strlen(str);
              warning C4244:
              '=' : conversion from
              'size_t' to 'short„
              , possible loss of data


    short len;
    len = (short) strlen(str);
그런 거 별 생각 없이 짠다고

뭐 큰 문제 있나요?
double d;
USHORT s = (USHORT) d;
size_t C = GetDataCount(…);
for(size_t i=0; i<C; ++i)
{
    SendSecurityPacket(…);
}

  int GetDataCount(…);
나는

얼마나 알고 있을까?
char a, b, c, t, r1, r2;
a = b = c = 100;

r1 = a + b – c;

t = a + b;
r2 = t – c;

if(r1 == r2)
    printf(“EQ”);
else
    printf(“NEQ”);
int compare(INT a, USHORT b)
{
    if(a > b) return 1;
    else if(a < b) return -1;
    else return 0;
}

printf(“%d”, compare(-1, -1));
int compare(INT a, UINT b)
{
    if(a > b) return 1;
    else if(a < b) return -1;
    else return 0;
}

printf(“%d”, compare(-1, 0));
int a = 6;
int b = -2;
printf(“%d”, a + b);

unsigned int c = -2;
printf(“%d”, a + c);

short d = -2;
printf(“%d”, a + d);

unsigned char e = -2;
printf(“%d”, a + e);
int a = 6, b = -2;
printf(“%d”, a / b);

unsigned int c = -2;
printf(“%d”, a / c);

short d = -2;
printf(“%d”, a / d);

unsigned char e = -2;
printf(“%d”, a / e);
제대로 배우는

C/C++ 정수 연산
정수 표현 방식
• Sign Bit
• One’s complement
• Two’s complement
Sign Bit
One’s complement
Two’s complement
Two’s complement
Two’s complement
Usual Arithmetic Conversions
•   If either operand is of type long double, the other operand is converted to
    type long double.
•   If the above condition is not met and either operand is of type double, the
    other operand is converted to type double.
•   If the above two conditions are not met and either operand is of type float,
    the other operand is converted to type float.
•   If the above three conditions are not met (none of the operands are of
    floating types), then integral conversions are performed on the operands as
    follows:
     – If either operand is of type unsigned long, the other operand is converted to type
       unsigned long.
     – If the above condition is not met and either operand is of type long and the other of
       type unsigned int, both operands are converted to type unsigned long.
     – If the above two conditions are not met, and either operand is of type long, the other
       operand is converted to type long.
     – If the above three conditions are not met, and either operand is of type unsigned int,
       the other operand is converted to type unsigned int.
     – If none of the above conditions are met, both operands are converted to type int.
Usual Arithmetic Conversions




unsigned long => ULONG
long + unsigned int => ULONG
long => LONG
unsigned int => UINT
ETC => INT
Sign Extend
short a = -3;
int b = a;

char a = -3;
USHORT b = a;
Zero Extend
UCHAR a = 3;
short b = a;

USHORT a = -4;
int b = a;
Preserve bit pattern
UINT a = -4;
int b = a;

int a = -4;
UINT b = a;

int a = -4;
short b = a;
Conversion Method
• 같은 사이즈는 닥치고 Preserve.
• 큰거에서 작은거는 무조건 Preserve.
• 작은거에서 큰거는 Signed는 Sign Extend,
  Unsigned는 Zero Extend.
Two’s Complement
int a = 6;
int b = -2;

int c = a + b;



                 int a = 6;
                 unsigned int b = -2;

                 int c = a + b;
Two’s Complement
int a = 6;
int b = -2;

int c = a / b;



                 int a = 6;
                 unsigned int b = -2;

                 int c = a / b;
정수연산 오류
정수 연산 오류
• Integer Overflow
• Sign Error
• Truncation Error
Integer Overflow
int compare(int a, int b)
{
    if(a > b) return 1;
    else if(a < b) return -1;
    return 0;
}


             int compare(int a, int b)
             {
                 return a – b;
             }
Integer Overflow

UINT sum(UINT *arr, int len)
{
    UINT s = 0;
    for(int i=0; i<len; ++i)
        s += arr[i];
    return s;
}
Sign Error

int size;
size = atoi(argv[1]);
char *buffer = malloc((size_t) size);
Sign Error
int off, len;

if(off > len – sizeof(type_name))
    goto error;



     int off, len;

     if(off + sizeof(type_name) > len)
         goto error;
Truncation Error

int a = USHRT_MAX + 1;
USHORT b = (USHORT) a;



            short a = 3000;
            char b = (char) a;
왜 어려울까?

__try
{
    int a = INT_MAX, b = 1;
    int c = a + b;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
    // ??
}
왜 어려울까?
int a = INT_MAX, b = 1;
int c = a + b;

char a = INT_MAX, b = 1;
int c = a + b; INT_MAX, b = 1;
 unsigned a =
 int c = a = INT_MAX, b = 1;
   short a + b;
   int c = a + b;
     long a = INT_MAX, b = 1;
     int c =aa=*INT_MAX, b = 1;
       char      b;
       int c = a * b;
정수 연산 똑바로 하라고 책까지
썼는데, 사서 읽어 보는 놈이 없
      눼... ㅠㅠ~

우리가 그냥 하나 만들어 주는게
좋겠어. 멍청한 애들 고생 안하
        게...
                     그래? 근데 나 코딩 안한지 엄청
                     오래 됐는데. 니가 만들어. ㅋㅋ~
종결자

SAFEINT
#include <safeint.h>
#include <limits.h>

using namespace msl::utilities;

int _tmain(int argc, _TCHAR* argv[])
{
    SafeInt<int> a(UCHAR_MAX + 1);
    char b = a;

    return 0;
}
SafeInt<int> a;
int b = 1;

a = INT_MAX;
int c = a + b;

SafeInt<int> a;

a = INT_MIN;
int c = a * 2;
void Function(size_t len) {}
SafeInt<int> len = -2;
Function(len);
SafeInt<int> a = UCHAR_MAX;
short b = a;
char c = a;
struct SafeIntPolicyPrintNExit {
    static void __stdcall
    SafeIntOnOverflow() {
        printf("overflown");
        exit(-1);
    }

     static void __stdcall
     SafeIntOnDivZero() {
         printf("divide by zeron");
         exit(-1);
     }
};
#define _SAFEINT_DEFAULT_ERROR_POLICY 
        SafeIntPolicyPrintNExit

#include <safeint.h>



SafeInt<int, SafeIntPolicyPrintNExit> a;
try
{
      SafeInt<int> a = UCHAR_MAX;

      short b = a;
      char c = a;
}
catch(SafeIntException &e)
{
    printf("%dn", e.m_code);
}
enum SafeIntError
{
    SafeIntNoError = 0,
    SafeIntArithmeticOverflow,
    SafeIntDivideByZero
};
SO WHAT?
short len;
len = strlen(str);
              warning C4244:
              '=' : conversion from
              'size_t' to 'short„
              , possible loss of data


      short len;
      len = (short) strlen(str);


    short len;
    len = SafeInt<short>(strlen(str));
for(int i=0; i<10; ++i) {}


   for(SafeInt<int> i=0; i<10; ++i) {}
감사합니다.

Weitere ähnliche Inhalte

Was ist angesagt?

7segment scetch
7segment scetch7segment scetch
7segment scetchBang Igo
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++Muhammad Hammad Waseem
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++Muhammad Hammad Waseem
 
Lab lect03 arith_control
Lab lect03 arith_controlLab lect03 arith_control
Lab lect03 arith_controlMPDS
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...Bilal Amjad
 
Assembly language-lab9
Assembly language-lab9Assembly language-lab9
Assembly language-lab9AjEcuacion
 
Lect 5 2d clipping
Lect 5 2d clippingLect 5 2d clipping
Lect 5 2d clippingmajicyoung
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to PointersMuhammad Hammad Waseem
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]ecko_disasterz
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in CSaket Pathak
 
Aosd2009 adams
Aosd2009 adamsAosd2009 adams
Aosd2009 adamsSAIL_QU
 

Was ist angesagt? (20)

Interesting facts on c
Interesting facts on cInteresting facts on c
Interesting facts on c
 
7segment scetch
7segment scetch7segment scetch
7segment scetch
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
 
Pointers
PointersPointers
Pointers
 
Lab lect03 arith_control
Lab lect03 arith_controlLab lect03 arith_control
Lab lect03 arith_control
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
 
verilog code
verilog codeverilog code
verilog code
 
Assembly language-lab9
Assembly language-lab9Assembly language-lab9
Assembly language-lab9
 
Ch06
Ch06Ch06
Ch06
 
Lect 5 2d clipping
Lect 5 2d clippingLect 5 2d clipping
Lect 5 2d clipping
 
Function recap
Function recapFunction recap
Function recap
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]
 
Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
Aosd2009 adams
Aosd2009 adamsAosd2009 adams
Aosd2009 adams
 
C aptitude
C aptitudeC aptitude
C aptitude
 

Andere mochten auch

Whitepaper Troubleshooting SSIS Failures and Performance using BI xPress
Whitepaper Troubleshooting SSIS Failures and Performance using BI xPressWhitepaper Troubleshooting SSIS Failures and Performance using BI xPress
Whitepaper Troubleshooting SSIS Failures and Performance using BI xPressMILL5
 
Ljiljana Siničković, “Bērnu labklājība – Priekšnosacījums Cilvēkkapitāla attī...
Ljiljana Siničković, “Bērnu labklājība – Priekšnosacījums Cilvēkkapitāla attī...Ljiljana Siničković, “Bērnu labklājība – Priekšnosacījums Cilvēkkapitāla attī...
Ljiljana Siničković, “Bērnu labklājība – Priekšnosacījums Cilvēkkapitāla attī...SOS Children'sVillages Latvia
 
Vienaudžu izpētes projekts "Patstāvīgas dzīves uzsākšana pēc ārpusģimenes apr...
Vienaudžu izpētes projekts "Patstāvīgas dzīves uzsākšana pēc ārpusģimenes apr...Vienaudžu izpētes projekts "Patstāvīgas dzīves uzsākšana pēc ārpusģimenes apr...
Vienaudžu izpētes projekts "Patstāvīgas dzīves uzsākšana pēc ārpusģimenes apr...SOS Children'sVillages Latvia
 
Martti Kemppainen, Work of Child Welfare Network in Finland
Martti Kemppainen, Work of Child Welfare Network in FinlandMartti Kemppainen, Work of Child Welfare Network in Finland
Martti Kemppainen, Work of Child Welfare Network in FinlandSOS Children'sVillages Latvia
 
Lauris Neikens Labklājības ministrija un NVO sadarbība
Lauris Neikens Labklājības ministrija un NVO sadarbībaLauris Neikens Labklājības ministrija un NVO sadarbība
Lauris Neikens Labklājības ministrija un NVO sadarbībaSOS Children'sVillages Latvia
 
Powerbi 130926080957-phpapp02
Powerbi 130926080957-phpapp02Powerbi 130926080957-phpapp02
Powerbi 130926080957-phpapp02MILL5
 
Sociālajam riskam pakļauto ģimeņu situācijas analīze Latvijā
Sociālajam riskam pakļauto ģimeņu situācijas analīze LatvijāSociālajam riskam pakļauto ģimeņu situācijas analīze Latvijā
Sociālajam riskam pakļauto ģimeņu situācijas analīze LatvijāSOS Children'sVillages Latvia
 
Transformation of thought1
Transformation of thought1Transformation of thought1
Transformation of thought1giovannadipede
 
prof. Dzintara Mozga prezentācija Bērnu labklājība kā Latvijas cilvēkkapitāla...
prof. Dzintara Mozga prezentācija Bērnu labklājība kā Latvijas cilvēkkapitāla...prof. Dzintara Mozga prezentācija Bērnu labklājība kā Latvijas cilvēkkapitāla...
prof. Dzintara Mozga prezentācija Bērnu labklājība kā Latvijas cilvēkkapitāla...SOS Children'sVillages Latvia
 
ĢIMEŅU STIPRINĀŠANAS SOCIĀLĀS REHABILITĀCIJAS PAKALPOJUMU KONCEPCIJA
ĢIMEŅU STIPRINĀŠANAS SOCIĀLĀS REHABILITĀCIJAS PAKALPOJUMU KONCEPCIJAĢIMEŅU STIPRINĀŠANAS SOCIĀLĀS REHABILITĀCIJAS PAKALPOJUMU KONCEPCIJA
ĢIMEŅU STIPRINĀŠANAS SOCIĀLĀS REHABILITĀCIJAS PAKALPOJUMU KONCEPCIJASOS Children'sVillages Latvia
 
The Forrester Wave of Self Service BI Platforms
The Forrester Wave of Self Service BI PlatformsThe Forrester Wave of Self Service BI Platforms
The Forrester Wave of Self Service BI PlatformsMILL5
 
scrapy+sphinx搭建搜索引擎
scrapy+sphinx搭建搜索引擎scrapy+sphinx搭建搜索引擎
scrapy+sphinx搭建搜索引擎Ping Yin
 

Andere mochten auch (15)

Whitepaper Troubleshooting SSIS Failures and Performance using BI xPress
Whitepaper Troubleshooting SSIS Failures and Performance using BI xPressWhitepaper Troubleshooting SSIS Failures and Performance using BI xPress
Whitepaper Troubleshooting SSIS Failures and Performance using BI xPress
 
Ljiljana Siničković, “Bērnu labklājība – Priekšnosacījums Cilvēkkapitāla attī...
Ljiljana Siničković, “Bērnu labklājība – Priekšnosacījums Cilvēkkapitāla attī...Ljiljana Siničković, “Bērnu labklājība – Priekšnosacījums Cilvēkkapitāla attī...
Ljiljana Siničković, “Bērnu labklājība – Priekšnosacījums Cilvēkkapitāla attī...
 
Vienaudžu izpētes projekts "Patstāvīgas dzīves uzsākšana pēc ārpusģimenes apr...
Vienaudžu izpētes projekts "Patstāvīgas dzīves uzsākšana pēc ārpusģimenes apr...Vienaudžu izpētes projekts "Patstāvīgas dzīves uzsākšana pēc ārpusģimenes apr...
Vienaudžu izpētes projekts "Patstāvīgas dzīves uzsākšana pēc ārpusģimenes apr...
 
Martti Kemppainen, Work of Child Welfare Network in Finland
Martti Kemppainen, Work of Child Welfare Network in FinlandMartti Kemppainen, Work of Child Welfare Network in Finland
Martti Kemppainen, Work of Child Welfare Network in Finland
 
Lauris Neikens Labklājības ministrija un NVO sadarbība
Lauris Neikens Labklājības ministrija un NVO sadarbībaLauris Neikens Labklājības ministrija un NVO sadarbība
Lauris Neikens Labklājības ministrija un NVO sadarbība
 
Powerbi 130926080957-phpapp02
Powerbi 130926080957-phpapp02Powerbi 130926080957-phpapp02
Powerbi 130926080957-phpapp02
 
Yyyyy
YyyyyYyyyy
Yyyyy
 
Sociālajam riskam pakļauto ģimeņu situācijas analīze Latvijā
Sociālajam riskam pakļauto ģimeņu situācijas analīze LatvijāSociālajam riskam pakļauto ģimeņu situācijas analīze Latvijā
Sociālajam riskam pakļauto ģimeņu situācijas analīze Latvijā
 
Sos zinotajs 2013/1
Sos zinotajs 2013/1Sos zinotajs 2013/1
Sos zinotajs 2013/1
 
SOS ziņotājs 2013/2
SOS ziņotājs 2013/2SOS ziņotājs 2013/2
SOS ziņotājs 2013/2
 
Transformation of thought1
Transformation of thought1Transformation of thought1
Transformation of thought1
 
prof. Dzintara Mozga prezentācija Bērnu labklājība kā Latvijas cilvēkkapitāla...
prof. Dzintara Mozga prezentācija Bērnu labklājība kā Latvijas cilvēkkapitāla...prof. Dzintara Mozga prezentācija Bērnu labklājība kā Latvijas cilvēkkapitāla...
prof. Dzintara Mozga prezentācija Bērnu labklājība kā Latvijas cilvēkkapitāla...
 
ĢIMEŅU STIPRINĀŠANAS SOCIĀLĀS REHABILITĀCIJAS PAKALPOJUMU KONCEPCIJA
ĢIMEŅU STIPRINĀŠANAS SOCIĀLĀS REHABILITĀCIJAS PAKALPOJUMU KONCEPCIJAĢIMEŅU STIPRINĀŠANAS SOCIĀLĀS REHABILITĀCIJAS PAKALPOJUMU KONCEPCIJA
ĢIMEŅU STIPRINĀŠANAS SOCIĀLĀS REHABILITĀCIJAS PAKALPOJUMU KONCEPCIJA
 
The Forrester Wave of Self Service BI Platforms
The Forrester Wave of Self Service BI PlatformsThe Forrester Wave of Self Service BI Platforms
The Forrester Wave of Self Service BI Platforms
 
scrapy+sphinx搭建搜索引擎
scrapy+sphinx搭建搜索引擎scrapy+sphinx搭建搜索引擎
scrapy+sphinx搭建搜索引擎
 

Ähnlich wie Safe int (20)

C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
Cbasic
CbasicCbasic
Cbasic
 
Cbasic
CbasicCbasic
Cbasic
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言
 
C programming
C programmingC programming
C programming
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
C tutorial
C tutorialC tutorial
C tutorial
 
Cbasic
CbasicCbasic
Cbasic
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
C Programming Language
C Programming LanguageC Programming Language
C Programming Language
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5
 
Vcs16
Vcs16Vcs16
Vcs16
 

Kürzlich hochgeladen

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Kürzlich hochgeladen (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Safe int

  • 3. int a, b, c; int d = a * b + c;
  • 4. short len; len = strlen(str); warning C4244: '=' : conversion from 'size_t' to 'short„ , possible loss of data short len; len = (short) strlen(str);
  • 5. 그런 거 별 생각 없이 짠다고 뭐 큰 문제 있나요?
  • 6.
  • 7. double d; USHORT s = (USHORT) d;
  • 8.
  • 9. size_t C = GetDataCount(…); for(size_t i=0; i<C; ++i) { SendSecurityPacket(…); } int GetDataCount(…);
  • 10.
  • 11.
  • 13. char a, b, c, t, r1, r2; a = b = c = 100; r1 = a + b – c; t = a + b; r2 = t – c; if(r1 == r2) printf(“EQ”); else printf(“NEQ”);
  • 14. int compare(INT a, USHORT b) { if(a > b) return 1; else if(a < b) return -1; else return 0; } printf(“%d”, compare(-1, -1));
  • 15. int compare(INT a, UINT b) { if(a > b) return 1; else if(a < b) return -1; else return 0; } printf(“%d”, compare(-1, 0));
  • 16. int a = 6; int b = -2; printf(“%d”, a + b); unsigned int c = -2; printf(“%d”, a + c); short d = -2; printf(“%d”, a + d); unsigned char e = -2; printf(“%d”, a + e);
  • 17. int a = 6, b = -2; printf(“%d”, a / b); unsigned int c = -2; printf(“%d”, a / c); short d = -2; printf(“%d”, a / d); unsigned char e = -2; printf(“%d”, a / e);
  • 19. 정수 표현 방식 • Sign Bit • One’s complement • Two’s complement
  • 25. Usual Arithmetic Conversions • If either operand is of type long double, the other operand is converted to type long double. • If the above condition is not met and either operand is of type double, the other operand is converted to type double. • If the above two conditions are not met and either operand is of type float, the other operand is converted to type float. • If the above three conditions are not met (none of the operands are of floating types), then integral conversions are performed on the operands as follows: – If either operand is of type unsigned long, the other operand is converted to type unsigned long. – If the above condition is not met and either operand is of type long and the other of type unsigned int, both operands are converted to type unsigned long. – If the above two conditions are not met, and either operand is of type long, the other operand is converted to type long. – If the above three conditions are not met, and either operand is of type unsigned int, the other operand is converted to type unsigned int. – If none of the above conditions are met, both operands are converted to type int.
  • 26. Usual Arithmetic Conversions unsigned long => ULONG long + unsigned int => ULONG long => LONG unsigned int => UINT ETC => INT
  • 27. Sign Extend short a = -3; int b = a; char a = -3; USHORT b = a;
  • 28. Zero Extend UCHAR a = 3; short b = a; USHORT a = -4; int b = a;
  • 29. Preserve bit pattern UINT a = -4; int b = a; int a = -4; UINT b = a; int a = -4; short b = a;
  • 30. Conversion Method • 같은 사이즈는 닥치고 Preserve. • 큰거에서 작은거는 무조건 Preserve. • 작은거에서 큰거는 Signed는 Sign Extend, Unsigned는 Zero Extend.
  • 31. Two’s Complement int a = 6; int b = -2; int c = a + b; int a = 6; unsigned int b = -2; int c = a + b;
  • 32. Two’s Complement int a = 6; int b = -2; int c = a / b; int a = 6; unsigned int b = -2; int c = a / b;
  • 34. 정수 연산 오류 • Integer Overflow • Sign Error • Truncation Error
  • 35. Integer Overflow int compare(int a, int b) { if(a > b) return 1; else if(a < b) return -1; return 0; } int compare(int a, int b) { return a – b; }
  • 36. Integer Overflow UINT sum(UINT *arr, int len) { UINT s = 0; for(int i=0; i<len; ++i) s += arr[i]; return s; }
  • 37. Sign Error int size; size = atoi(argv[1]); char *buffer = malloc((size_t) size);
  • 38. Sign Error int off, len; if(off > len – sizeof(type_name)) goto error; int off, len; if(off + sizeof(type_name) > len) goto error;
  • 39. Truncation Error int a = USHRT_MAX + 1; USHORT b = (USHORT) a; short a = 3000; char b = (char) a;
  • 40. 왜 어려울까? __try { int a = INT_MAX, b = 1; int c = a + b; } __except(EXCEPTION_EXECUTE_HANDLER) { // ?? }
  • 41. 왜 어려울까? int a = INT_MAX, b = 1; int c = a + b; char a = INT_MAX, b = 1; int c = a + b; INT_MAX, b = 1; unsigned a = int c = a = INT_MAX, b = 1; short a + b; int c = a + b; long a = INT_MAX, b = 1; int c =aa=*INT_MAX, b = 1; char b; int c = a * b;
  • 42. 정수 연산 똑바로 하라고 책까지 썼는데, 사서 읽어 보는 놈이 없 눼... ㅠㅠ~ 우리가 그냥 하나 만들어 주는게 좋겠어. 멍청한 애들 고생 안하 게... 그래? 근데 나 코딩 안한지 엄청 오래 됐는데. 니가 만들어. ㅋㅋ~
  • 44.
  • 45. #include <safeint.h> #include <limits.h> using namespace msl::utilities; int _tmain(int argc, _TCHAR* argv[]) { SafeInt<int> a(UCHAR_MAX + 1); char b = a; return 0; }
  • 46. SafeInt<int> a; int b = 1; a = INT_MAX; int c = a + b; SafeInt<int> a; a = INT_MIN; int c = a * 2;
  • 47. void Function(size_t len) {} SafeInt<int> len = -2; Function(len);
  • 48. SafeInt<int> a = UCHAR_MAX; short b = a; char c = a;
  • 49. struct SafeIntPolicyPrintNExit { static void __stdcall SafeIntOnOverflow() { printf("overflown"); exit(-1); } static void __stdcall SafeIntOnDivZero() { printf("divide by zeron"); exit(-1); } };
  • 50. #define _SAFEINT_DEFAULT_ERROR_POLICY SafeIntPolicyPrintNExit #include <safeint.h> SafeInt<int, SafeIntPolicyPrintNExit> a;
  • 51. try { SafeInt<int> a = UCHAR_MAX; short b = a; char c = a; } catch(SafeIntException &e) { printf("%dn", e.m_code); }
  • 52. enum SafeIntError { SafeIntNoError = 0, SafeIntArithmeticOverflow, SafeIntDivideByZero };
  • 54. short len; len = strlen(str); warning C4244: '=' : conversion from 'size_t' to 'short„ , possible loss of data short len; len = (short) strlen(str); short len; len = SafeInt<short>(strlen(str));
  • 55. for(int i=0; i<10; ++i) {} for(SafeInt<int> i=0; i<10; ++i) {}