SlideShare uma empresa Scribd logo
1 de 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) {}
감사합니다.

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Destaque

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
 

Destaque (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搭建搜索引擎
 

Semelhante a 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 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
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 

Último

Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 

Último (20)

Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 

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) {}