From: Hewlett Date: Wed, 4 Dec 2024 05:25:51 +0000 (+0900) Subject: add toy.cpp X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F96%2F315696%2F1;p=scm%2Fcode-review-bot-test.git add toy.cpp Change-Id: I5197490b9e8ad16eec45e01449d4f237ea8a1a23 --- diff --git a/toy.cpp b/toy.cpp new file mode 100644 index 0000000..27646bd --- /dev/null +++ b/toy.cpp @@ -0,0 +1,125 @@ +/* contains all autofix checkers */ + +#include +#include +#include + +/********************* ALLOC_SIZE_MISMATCH *********************/ +struct Person { + int age; + char name[20]; +}; + +struct Person *alloc_size_mismatch(void) { + struct Person *ptr; + ptr = (struct Person *)malloc(sizeof(ptr)); + return ptr; +} +/****************************************************************/ + +/********************* BAD_ALLOC_ARITHMETIC *********************/ +char *bad_alloc_arithmetic(char *str, int len) { + char *res = malloc(len) + 1; + + memcpy(res, str, len); + res[len] = '\0'; + + return res; +} +/****************************************************************/ + +/********************* BAD_COPY_PASTE *********************/ +int callee(int x) { return x * x + 1; } + +int bad_copy_paste(int a, int b, int x) { + int result = 0; + + // copied + if (b > 0) { + result = callee(a) + callee(x); // auto fix + } + + // maybe origin + if (a > 0) { + result = callee(a) + callee(x); + } + + return result; +} +/****************************************************************/ + +/********************* BAD_SIZEOF *********************/ +void bad_sizeof() { + short s; + memset(&s, 0, sizeof(&s)); // auto fix +} +/****************************************************************/ + +/********************* MEMSET_SIZE_MISMATCH *********************/ +typedef struct { + int x; + int y; +} FooBarStruct; + +void memset_size_mismatch() { + FooBarStruct fbs; + memset(&fbs, 0, 4); // auto fix +} +/****************************************************************/ + +/********************* NO_CAST.INTEGER_DIVISION *********************/ +float divv(int a, int b) { return (a / b); } +void no_cast_integer_division() { printf("%0.2f\n", divv(10, 4)); } +/****************************************************************/ + +/********************* NO_CAST.INTEGER_OVERFLOW *********************/ +long mult(int a, int b) { return (a * b); } // auto fix +void no_cast_integer_overflow() { printf("%ld\n", mult(0x7FFFFFFF, 2)); } +/****************************************************************/ + +/********************* NO_EFFECT *********************/ +void no_effect() { + int *ptr1, *ptr2; + ptr1 = new int(10); + ptr2 = new int(15); + delete ptr1, ptr2; +} +/****************************************************************/ + +/********************* SIZEOF_POINNTER_TYPE *********************/ +struct buffer { + char b[100]; +}; + +void sizeof_pointer_type() { + struct buffer buf; + memset(&buf, 0, sizeof(&buf)); + + struct buffer *pbuf; + pbuf = (struct buffer *)malloc(sizeof(struct buffer *)); // auto fix +} +/****************************************************************/ + +/********************* SIZEOF_POINTER_TYPE.CHAR *********************/ +void sizeof_pointer_type_char(int len, char *pc) { + pc = (char *)malloc(len * sizeof(pc)); +} +/****************************************************************/ + +/********************* UNREACHABLE_CODE.NO_PATH *********************/ +int unreachable_code_no_path(int a) { + int ret = 0; + switch (a) { + case 1: + ret = 2; + break; + ret = 3; // unreachable + default: + ret = -1; + break; + } + return ret; + ret = ret * a; // unreachable +} +/****************************************************************/ +