brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.2 KiB · 688e414 Raw
107 lines · cpp
1// RUN: %check_clang_tidy -std=c++11-or-later %s bugprone-not-null-terminated-result %t -- \2// RUN: -- -I %S/Inputs/not-null-terminated-result3 4#include "not-null-terminated-result-cxx.h"5 6#define __STDC_LIB_EXT1__ 17#define __STDC_WANT_LIB_EXT1__ 18 9void bad_wmemchr_1(wchar_t *position, const wchar_t *src) {10  position = (wchar_t *)wmemchr(src, L'\0', wcslen(src));11  // CHECK-MESSAGES: :[[@LINE-1]]:45: warning: the length is too short to include the null terminator [bugprone-not-null-terminated-result]12  // CHECK-FIXES: position = wcschr(src, L'\0');13}14 15void good_wmemchr_1(wchar_t *pos, const wchar_t *src) {16  pos = wcschr(src, L'\0');17}18 19void bad_wmemchr_2(wchar_t *position) {20  position = (wchar_t *)wmemchr(L"foobar", L'\0', 6);21  // CHECK-MESSAGES: :[[@LINE-1]]:51: warning: the length is too short to include the null terminator [bugprone-not-null-terminated-result]22  // CHECK-FIXES: position = wcschr(L"foobar", L'\0');23}24 25void good_wmemchr_2(wchar_t *pos) {26  pos = wcschr(L"foobar", L'\0');27}28 29 30void bad_wmemmove(const wchar_t *src) {31  wchar_t dest[13];32  wmemmove(dest, src, wcslen(src));33  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemmove' is not null-terminated [bugprone-not-null-terminated-result]34  // CHECK-FIXES: wchar_t dest[14];35  // CHECK-FIXES-NEXT: wmemmove_s(dest, 14, src, wcslen(src) + 1);36}37 38void good_wmemmove(const wchar_t *src) {39  wchar_t dst[14];40  wmemmove_s(dst, 13, src, wcslen(src) + 1);41}42 43void bad_wmemmove_s(wchar_t *dest, const wchar_t *src) {44  wmemmove_s(dest, 13, src, wcslen(src));45  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemmove_s' is not null-terminated [bugprone-not-null-terminated-result]46  // CHECK-FIXES: wmemmove_s(dest, 13, src, wcslen(src) + 1);47}48 49void good_wmemmove_s_1(wchar_t *dest, const wchar_t *src) {50  wmemmove_s(dest, 13, src, wcslen(src) + 1);51}52 53int bad_wcsncmp_1(wchar_t *wcs0, const wchar_t *wcs1) {54  return wcsncmp(wcs0, wcs1, (wcslen(wcs0) + 1));55  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: comparison length is too long and might lead to a buffer overflow [bugprone-not-null-terminated-result]56  // CHECK-FIXES: return wcsncmp(wcs0, wcs1, (wcslen(wcs0)));57}58 59int bad_wcsncmp_2(wchar_t *wcs2, const wchar_t *wcs3) {60  return wcsncmp(wcs2, wcs3, 1 + wcslen(wcs2));61  // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: comparison length is too long and might lead to a buffer overflow [bugprone-not-null-terminated-result]62  // CHECK-FIXES: return wcsncmp(wcs2, wcs3, wcslen(wcs2));63}64 65int good_wcsncmp_1_2(wchar_t *wcs4, const wchar_t *wcs5) {66  return wcsncmp(wcs4, wcs5, wcslen(wcs4));67}68 69int bad_wcsncmp_3(wchar_t *wcs6) {70  return wcsncmp(wcs6, L"string", 7);71  // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: comparison length is too long and might lead to a buffer overflow [bugprone-not-null-terminated-result]72  // CHECK-FIXES: return wcsncmp(wcs6, L"string", 6);73}74 75int good_wcsncmp_3(wchar_t *wcs7) {76  return wcsncmp(wcs7, L"string", 6);77}78 79void bad_wcsxfrm_1(const wchar_t *long_source_name) {80  wchar_t long_destination_array_name[13];81  wcsxfrm(long_destination_array_name, long_source_name,82          wcslen(long_source_name));83  // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: the result from calling 'wcsxfrm' is not null-terminated [bugprone-not-null-terminated-result]84  // CHECK-FIXES: wchar_t long_destination_array_name[14];85  // CHECK-FIXES-NEXT: wcsxfrm(long_destination_array_name, long_source_name,86  // CHECK-FIXES-NEXT: wcslen(long_source_name) + 1);87}88 89void good_wcsxfrm_1(const wchar_t *long_source_name) {90  wchar_t long_destination_array_name[14];91  wcsxfrm(long_destination_array_name, long_source_name,92          wcslen(long_source_name) + 1);93}94 95void bad_wcsxfrm_2() {96  wchar_t long_destination_array_name1[16];97  wcsxfrm(long_destination_array_name1, L"long_source_name", 16);98  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wcsxfrm' is not null-terminated [bugprone-not-null-terminated-result]99  // CHECK-FIXES: wchar_t long_destination_array_name1[17];100  // CHECK-FIXES: wcsxfrm(long_destination_array_name1, L"long_source_name", 17);101}102 103void good_wcsxfrm_2() {104  wchar_t long_destination_array_name2[17];105  wcsxfrm(long_destination_array_name2, L"long_source_name", 17);106}107