1. C++ / Говнокод #28928

    0

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    for(int i = 0; i < p.mDict.TblSize; i++)
    		for(auto *node = p.mDict.table[i]; node; node = node->n)
    			for(int j = 0; j < node->v.TblSize; j++)
    				for(int k = 0; k < node->v.table[j].count; k++ )
    					if(node->v.table[j][k].v)
    						Log("Section %s: unused config key %s = %s\n", node->k, node->v.table[j][k].k, node->v.table[j][k].v);

    mittorn, 14 Марта 2024

    Комментарии (2)
  2. C++ / Говнокод #28908

    0

    1. 1
    2. 2
    3. 3
    4. 4
    int main(int Argcina, char** Argvina) {
        int tse_odna_strana_dlya_vseh = (char**)Argcina - Argvina;
        return 0;
    }

    Milochocientos, 16 Января 2024

    Комментарии (12)
  3. C++ / Говнокод #28893

    0

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    switch (lol) {
        case E::Foo:
            return 1;
        case E::Bar:
        default:
            return 0;
        case E::Baz:
            return 2;
    }

    Вы знали, что так можно?
    Надо запустить пиваса.

    3_dar, 22 Декабря 2023

    Комментарии (8)
  4. C++ / Говнокод #28892

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    // В преддверии нового года на сайте PVS-Studio увеличилось количества C++ говнокода.
    // Во-первых, вышла подбора багов за год, а во-вторых, квиз на поиск багов.
    // Первый пример кода из квиза для разминки:
    
    void  UObject::setDeltaPitch(const UMatrix &gizmo) {
      ....
      if (_fpzero(amount, eps))
        return
      rotateAccum.setAnglesXYZ(axis);
      ....
    }

    Если заинтересовались, то приятного чтения и удачи в поисках говнокодистых багов. Вас ждёт говнокод первого сорта.
    Топ 10 ошибок в C и С++ проектах в 2023 году - https://pvs-studio.ru/ru/blog/posts/cpp/1092/
    С++ квиз от PVS-Studio и Сергея Кушниренко - https://pvs-studio.ru/ru/blog/quest/kushnirenko_quiz/
    А если что-то останется непонятным в квизе, то есть его разбор - https://pvs-studio.ru/ru/blog/posts/cpp/1091/
    С наступающим НГ!

    Andrey_Karpov, 22 Декабря 2023

    Комментарии (37)
  5. C++ / Говнокод #28882

    0

    1. 1
    2. 2
    3. 3
    // Конкурс для C++ программистов от команды PVS-Studio
    // Уникальная возможность получить приз за говнокод с багом!
    // https://habr.com/ru/companies/pvs-studio/articles/775388/

    Поделитесь своим кодом с ошибкой и участвуйте в розыгрыше 10 книг "Вредные советы для C++ программистов". Подробности в публикации на Хабре: Конкурс для C++ программистов и их любимых багов - https://habr.com/ru/companies/pvs-studio/articles/775388/

    Andrey_Karpov, 21 Ноября 2023

    Комментарии (4)
  6. C++ / Говнокод #28881

    0

    1. 1
    2. 2
    3. 3
    4. 4
    bool isDate(int d, int m, int y)
    {
    	return m > 0 && m < 13 ? d > 0 && d < 29 + (m - 2 ? 2 + (m + m / 8) % 2 : !(y % 4) && (y % 100) || !(y % 400)) : 0;
    }

    sprog, 12 Ноября 2023

    Комментарии (7)
  7. C++ / Говнокод #28858

    +1

    1. 001
    2. 002
    3. 003
    4. 004
    5. 005
    6. 006
    7. 007
    8. 008
    9. 009
    10. 010
    11. 011
    12. 012
    13. 013
    14. 014
    15. 015
    16. 016
    17. 017
    18. 018
    19. 019
    20. 020
    21. 021
    22. 022
    23. 023
    24. 024
    25. 025
    26. 026
    27. 027
    28. 028
    29. 029
    30. 030
    31. 031
    32. 032
    33. 033
    34. 034
    35. 035
    36. 036
    37. 037
    38. 038
    39. 039
    40. 040
    41. 041
    42. 042
    43. 043
    44. 044
    45. 045
    46. 046
    47. 047
    48. 048
    49. 049
    50. 050
    51. 051
    52. 052
    53. 053
    54. 054
    55. 055
    56. 056
    57. 057
    58. 058
    59. 059
    60. 060
    61. 061
    62. 062
    63. 063
    64. 064
    65. 065
    66. 066
    67. 067
    68. 068
    69. 069
    70. 070
    71. 071
    72. 072
    73. 073
    74. 074
    75. 075
    76. 076
    77. 077
    78. 078
    79. 079
    80. 080
    81. 081
    82. 082
    83. 083
    84. 084
    85. 085
    86. 086
    87. 087
    88. 088
    89. 089
    90. 090
    91. 091
    92. 092
    93. 093
    94. 094
    95. 095
    96. 096
    97. 097
    98. 098
    99. 099
    100. 100
    void text::draw()
    {
    	CmdWinGUI::draw();
    	COORD temp = startCo;		//Разный мусор
    	COORD tempEnd = startCo;
    	int16_t _al = 0;
    	int16_t _an = 0;
    	int16_t remain = 0;
    	uint8_t a = 0;
    	bool fullRow = 1;	//Флаги, не изменять
    	bool alignPrepare = 0;
    	if (tableLine)
    	{
    		temp.Y = temp.Y + lineShift + sepShift;
    	}
    	if (!tableFormat)
    	{
    		if (alignSetting == ALIGN_C)
    		{
    			_al = sizeCo.X/2;
    			for (uint8_t a = 0;a < 0xFF;++a)	//Измеряем длину текстового поля
    			{
    				if (textBuffer[a] == NULL)
    					break;
    				++remain;
    			}
    			temp.X=startCo.X+ _al-remain/2;	//Корректируем курсор в соответствии с расчётами
    			if (temp.X < startCo.X)			//Если предыдущие вычисления дали ошибку - выход за предел рамки слева
    				temp.X = startCo.X;			//Корректируем
    		}
    		for (;;)
    		{
    			SetConsoleCursorPosition(hCon, temp);
    			if (textBuffer[a] == NULL)
    				break;
    			printf("%c", textBuffer[a]);
    			++temp.X;
    			++a;
    			if (temp.X == startCo.X + sizeCo.X - 1) //Проверяем достигло ли рамки, иначе обрываем цикл
    				break;
    		}
    		return;
    	}
    	for (uint8_t b = 0;b<=filledRows;++b)
    	{
    		if (!fullRow)
    		{
    			temp.X = tempEnd.X;
    		}
    		tempEnd.X += tableRowSize[b] - 1;
    		if (alignSetting == ALIGN_C)
    		{
    			--temp.X;
    			//Предварительные расчеты смещения
    			if (!alignPrepare)
    			{
    				for (SHORT a = temp.X;a < 0xFF;++a)
    				{
    					if (a == tempEnd.X) //Проверяем достигло ли рамки, иначе обрываем цикл
    					{
    						--_an;
    						break;		//половина длины поля таблицы
    					}
    					++_an;
    				}
    				for (uint8_t a = 0;a < 0xFF;++a)
    				{
    					if (tableText[b][a] == NULL)
    					{
    						_al = a;	//половина длины слова
    						break;
    					}
    				}
    				alignPrepare = 1;	//Подымаем флаг готовности
    			}
    			//Коррекция положения курсора в соответствии с результатами блока предварительных расчётов
    			temp.X += abs(_an / 2) - abs(_al / 2);
    			_an = 0;
    		}for (;;){
    			SetConsoleCursorPosition(hCon, temp);	//Ставим курсор в начало/середину/конец поля
    			if (tableText[b][a] == NULL)
    			{
    				fullRow = 0;	//При окончании поля текста до заполнения поля снимаем флаг заполнения поля
    				break;
    			}
    			++temp.X;
    			if (temp.X == tempEnd.X) //Проверяем достигло ли рамки, иначе обрываем цикл
    			{
    				fullRow = 1;	//При окончании поля в таблице до завершения поля текста прекращаем вывод и выходим из цикла
    				break;
    			}
    			printf("%c", tableText[b][a]);
    			++a;
    		}
    		a = 0;
    		alignPrepare = 0;	//Сбрасываем флаг для следующей итерации
    	}
    	if (isChanged)
    		this->editFlagSwitch();
    }

    Рендер текста с учётом коллизии слева и справа

    siclown, 30 Сентября 2023

    Комментарии (0)
  8. C++ / Говнокод #28857

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    #include "Windows.h"
    void line::draw()
    {
    	CmdWinGUI::draw();
    	uint8_t modB = 0; //modeByte
    	COORD tempSt = lineStart;
    	COORD tempEn = lineEnd;
    	SHORT dX = abs(lineEnd.X - lineStart.X);
    	SHORT dY = abs(lineEnd.Y - lineStart.Y);
    	SHORT sX = lineStart.X < lineEnd.X ? 1 : -1;
    	SHORT sY = lineStart.Y < lineEnd.Y ? 1 : -1;
    	SHORT dErr = dX - dY;
    	SHORT dErr2 = 0;
    	if (scan)
    	{
    		SetConsoleCursorPosition(hCon, lineStart);
    		for (uint8_t a = 0;a <= lineEnd.X-lineStart.X;++a)
    		{
    			modB = lineAtrBuffer[a];
    			SetConsoleTextAttribute(hCon, modB);
    			printf("%c", lineSymBuffer[a]);
    		}
    		if (isChanged)
    			this->editFlagSwitch();
    		return;
    	}
    	for (;;)
    	{
    		SetConsoleCursorPosition(hCon, tempSt);
    		printf("%c", lineSym);
    		if (tempSt.X == tempEn.X&&tempSt.Y == tempEn.Y)
    			break;
    		dErr2 = dErr * 2;
    		if (dErr2 > -dY)
    		{
    			dErr -= dY;
    			tempSt.X += sX;
    		}
    		if (dErr2 < dX)
    		{
    			dErr += dX;
    			tempSt.Y += sY;
    		}
    	}
    	if (isChanged)
    		this->editFlagSwitch();
    }

    Отрисовка произвольной графической линии в консоли

    siclown, 30 Сентября 2023

    Комментарии (1)
  9. C++ / Говнокод #28846

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    char *SomeGlobalPointer {};
    
    void foo()
    {
      SomeGlobalPointer = new char[1024];
    }
    
    int main()
    {
      foo();
    
      if (!SomeGlobalPointer)
      {
        delete[] SomeGlobalPointer;
      }
    
      return 0;
    }

    Отсюдова:

    https://pvs-studio.ru/ru/blog/posts/cpp/1068/

    CEHT9I6PbCKuu_nemyx, 13 Сентября 2023

    Комментарии (11)
  10. C++ / Говнокод #28841

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    auto objs = it.get_objects();
    
                switch(objs.size())
                {
                    case 1:     sent = _out_stream.send_message(objs[0]);
                                break;
                    case 2:     sent = _out_stream.send_message(objs[0], objs[1]);
                                break;
                    case 3:     sent = _out_stream.send_message(objs[0], objs[1], objs[2]);
                                break;
                    case 4:     sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3]);
                                break;
                    case 5:     sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4]);
                                break;
                    case 6:     sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5]);
                                break;
                    case 7:     sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6]);
                                break;
                    case 8:     sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7]);
                                break;
                    case 9:     sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8]);
                                break;
                    case 10:    sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8], objs[9]);
                                break;
                    case 11:    sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8], objs[9], objs[10]);
                                break;
                    case 12:    sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8], objs[9], objs[10], objs[11]);
                                break;
                    case 13:    sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8], objs[9], objs[10], objs[11],
                                                                objs[12]);
                                break;
                    case 14:    sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8], objs[9], objs[10], objs[11],
                                                                objs[12], objs[13]);
                                break;
                    case 15:    sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8], objs[9], objs[10], objs[11],
                                                                objs[12], objs[13], objs[14]);
                                break;
                    case 16:    sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8], objs[9], objs[10], objs[11],
                                                                objs[12], objs[13], objs[14], objs[15]);
                                break;
                    case 17:    sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8], objs[9], objs[10], objs[11],
                                                                objs[12], objs[13], objs[14], objs[15], objs[16]);
                                break;
                    case 18:    sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8], objs[9], objs[10], objs[11],
                                                                objs[12], objs[13], objs[14], objs[15], objs[16], objs[17]);
                                break;
                    case 19:    sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8], objs[9], objs[10], objs[11],
                                                                objs[12], objs[13], objs[14], objs[15], objs[16], objs[17],
                                                                objs[18]);
                                break;
                    case 20:    sent = _out_stream.send_message(objs[0], objs[1], objs[2], objs[3], objs[4], objs[5],
                                                                objs[6], objs[7], objs[8], objs[9], objs[10], objs[11],
                                                                objs[12], objs[13], objs[14], objs[15], objs[16], objs[17],
                                                                objs[18], objs[19]);
                                break;
                    default:
                        fprintf(stderr, "error: Too much attached objects (%lu), talk to developer\n", objs.size());
                        return false;
                }
    
                if (!sent)
                {
                    fprintf(stderr, "error: Unable to send message to output pipe\n");
                    return false;
                }

    Меня заставили это сделать, потому что по задумке число вложенных объектов известно на этапе разработки, а у меня - нет.

    YpaHeLI_, 31 Августа 2023

    Комментарии (32)