1. Лучший говнокод

    В номинации:
    За время:
  2. Си / Говнокод #28292

    +2

    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
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define NUMARGS(type,...)  (sizeof((type[]){__VA_ARGS__})/sizeof(type))
    #define xDEF(type) typedef struct {size_t size; type* arr;  }  xARR(type)
    #define xARR(type) jArr_ ## type
    
    #define A(type, ...) (xARR(type)) {NUMARGS(type,__VA_ARGS__), (type[]){__VA_ARGS__}}
    
    #define _FOR(type, item, Arr) type item=Arr.arr[0]; for(size_t I = 0; I < Arr.size; item=Arr.arr[++I] ) 
    // MSVC
    #define xFOR(type, item, array) do{  _FOR(type, item, array) {
    // GCC, Clang
    #define FOR(item, array)        do{ __auto_type Arr=array; _FOR(__auto_type, item, Arr) {
        
    #define NEXT }} while(0);
    
    #define OfArray(type,arr) (xARR(type)){sizeof(arr)/sizeof(type), arr }
    
    typedef struct {
        char *name;
        int     id;
    } Entry;
    
    
    typedef struct {const char *name;} Str;
    typedef struct {int x[2]; } Pair;
    
    xDEF(Entry);
    xDEF(Str);
    xDEF(Pair);
    xDEF(int);
    
    void printEntry(xARR(Entry) entries)
    {
        xFOR(Entry, e, entries)
            printf("%s %d \n", e.name, e.id);
        NEXT
    }
    
    void printSquares(xARR(int) ints)
    {
        FOR(v, ints)
            printf("%d²=%d\n", v,(int) pow(v,2.));
        NEXT
    }
    
    int main(void)
    {
        xARR(Entry) e = A(Entry, {"one",1}, {"two",2}, {"three",3});
        printEntry(e);
        
        puts("_______________________________________");
        
        // можно передавать в метод непосредственно аргуметом
        printSquares( A(int, 3, 7, 5, 4) );
        puts("_______________________________________");    
        
        int nums[]={4,3,2,1};
        // можно использовать ранее объявленный массив
        printSquares(OfArray(int,nums));
        
        // можно итерироватьcя по ранее объявленному массиву
        FOR(i, OfArray(int, nums))
            printf("%d-",i);
        NEXT
        
        puts("\n_______________________________________");
        
        //вложенные циклы:
        for (int k=1;k<3;k++)
            FOR(i, A(Str, "kakoi", "bagor"))    
                FOR(j, A(int, 1111,2222,3333))
                    printf("%d %s %d\n", k, i.name, j);
                NEXT
            NEXT
        
        puts("_______________________________________");
        
        FOR(v, A(Pair, {1,2}, {11,12}, {20,21}))
            printf("%d,%d\n", v.x[0], v.x[1]);
        NEXT
        puts("_______________________________________");    
        //проблема пустого варарга
        FOR(j, A(int))
            printf("%d\n", j);
        NEXT    
        return(0);
    }

    https://godbolt.org/z/o9Tv9EvGx

    Довёл for~each до ума.

    3.14159265, 23 Июля 2022

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

    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
    #define _CRT_SECURE_NO_WARNINGS
    #pragma comment(lib, "ntdll.lib")
    
    #include <Windows.h>
    #include <string>
    
    #define FLG_HEAP_ENABLE_TAIL_CHECK 0x10
    #define FLG_HEAP_ENABLE_FREE_CHECK 0x20
    #define FLG_HEAP_VALIDATE_PARAMETERS 0x40
    #define NT_GLOBAL_FLAG_DEBUGGED (FLG_HEAP_ENABLE_TAIL_CHECK | FLG_HEAP_ENABLE_FREE_CHECK | FLG_HEAP_VALIDATE_PARAMETERS)
    
    typedef NTSTATUS(NTAPI* pfnNtSetInformationThread)(
    	_In_ HANDLE	ThreadHandle,
    	_In_ ULONG ThreadInformationClass,
    	_In_ PVOID ThreadInformation,
    	_In_ ULONG ThreadInformationLenght);
    
    const ULONG ThreadHideFromDebugger = 0x11;
    
    typedef NTSTATUS(NTAPI* pfnNtQueryInformationProcess)(
    	_In_ HANDLE	ProcessHandle,
    	_In_ ULONG ProcessInformationClass,
    	_In_ PVOID ProcessInformation,
    	_In_ ULONG ProcessInformationLenght,
    	_Out_opt_ PULONG ReturnLenght);
    
    const UINT ProcessDebugPort = 7;
    
    void HideFromDebugger()
    {
    	HMODULE hNtDll = LoadLibrary("ntdll.dll");
    
    	if (!hNtDll)
    		throw std::exception("can't load kernel");
    
    	pfnNtSetInformationThread NtSetInformatioThread = (pfnNtSetInformationThread)
    		GetProcAddress(hNtDll, "NtSetInformationThread");
    
    	NTSTATUS status = NtSetInformatioThread(GetCurrentThread(), ThreadHideFromDebugger, NULL, NULL);
    }
    
    PVOID GetPEB()
    {
    	return (PVOID)__readfsword(0x0C * sizeof(PVOID));
    }
    
    int main()
    {
    	pfnNtQueryInformationProcess NtQueryInformationProcess = nullptr;
    	NTSTATUS status;
    	DWORD IsDebuggerPresent = 0;
    	HMODULE hNtDll = LoadLibrary("ntdll.dll");
    
    	if (!hNtDll)
    		throw std::exception("can't load kernel");
    	
    	NtQueryInformationProcess = (pfnNtQueryInformationProcess)GetProcAddress(hNtDll, "NtQueryInformationProcess");
    	void HideFromDebugger();
    
    	while (true)
    	{
    		PVOID pPEB = GetPEB();
    		DWORD offsetNtGlobalFlag = 0x68;
    		DWORD NtGlobalFlag = (DWORD)((PBYTE)pPEB + offsetNtGlobalFlag);
    
    		NTSTATUS stat = NtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
    			&IsDebuggerPresent, sizeof(DWORD), NULL);
    
    		if ((NtGlobalFlag & NT_GLOBAL_FLAG_DEBUGGED) || (stat == 0x00000000 && IsDebuggerPresent != 0))
    		{
    			MessageBox(NULL, "Close your fucking debuger!", "FUCK YOU", MB_OK);
    			return -1;
    		}
    	}
    
    	return 0;
    }

    PVOID, 23 Мая 2022

    Комментарии (26)
  4. SQL / Говнокод #28161

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    -- Теперь мы можем легко получить отчёт по продажам на прошлую дату:
    
    DELIMITER ;
    BEGIN;
    CALL set_prot_snapshot_date('2018-10-09 17:23:47', NULL, -1);
    SELECT NOW() report_time, d.date, SUM(p.amount * p.price) sum
    FROM docs d
    INNER JOIN doc_pos p ON d.id = p.doc_id
    GROUP BY d.date;
    ROLLBACK;

    https://habr.com/ru/post/425769/
    Как научить MySQL заглядывать в прошлое

    ISO, 09 Мая 2022

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

    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
    #include <iostream>
    #include <vector>
    #include <thread>
    int f()
    {
        static int i = 0;
        synchronized { // begin synchronized block
            std::cout << i << " -> ";
            ++i;       // each call to f() obtains a unique value of i
            std::cout << i << '\n';
            return i; // end synchronized block
        }
    }
    int main()
    {
        std::vector<std::thread> v(10);
        for(auto& t: v)
            t = std::thread([]{ for(int n = 0; n < 10; ++n) f(); });
        for(auto& t: v)
            t.join();
    }
    
    0 -> 1
    1 -> 2
    2 -> 3
    ...
    99 -> 100

    https://en.cppreference.com/w/cpp/language/transactional_memory

    kcalbCube, 27 Апреля 2022

    Комментарии (26)
  6. JavaScript / Говнокод #27905

    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
    export type Maybe<T> = null | undefined | T;
    
    export interface Path {
        readonly prev: Path | undefined;
        readonly key: string | number;
        readonly typename: string | undefined;
    }
    
    /**
     * Given a Path and a key, return a new Path containing the new key.
     */
    export function addPath(
        prev: Readonly<Path> | undefined,
        key: string | number,
        typename: string | undefined,
    ): Path {
        return { prev, key, typename };
    }
    
    /**
     * Given a Path, return an Array of the path keys.
     */
    export function pathToArray(
        path: Maybe<Readonly<Path>>,
    ): Array<string | number> {
        let curr = path;
        let flattened = [];
        while (curr) {
            flattened.push(curr.key);
            curr = curr.prev;
        }
        //flattened.reverse();
        return flattened;
    }
    
    function main() {
        let pathArray = pathToArray({
            key: "path",
            prev: undefined,
            typename: undefined,
        });
        for (let x of pathArray) {
            print(x);
        }
    }

    последний коммит позволяет скомпилить и выполнить данный код. это невиданный прогресс в компиляторе :)

    и уже традиционный вопрос ... а ты там можешь наговнокодить на С/C++?

    ASD_77, 31 Декабря 2021

    Комментарии (26)
  7. Куча / Говнокод #27860

    +1

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    Можно ли считать говнокодом (говноAPI) правильно работающую, но незадокументированную особенность API?
    Например у вьюхи есть свойство isOpen, которое может быть задано (true/false) а может быть не задано (undefined). 
    Первое нужно для управления видимостью в реактивном стиле, второе предполагает что разработчик будет
    управлять видимостью через хендлы вьюхи. И оно так и работает - если isOpen=undefined, то этот проп просто игнорируется
    при обновлении вьюхи (чтобы не допустить конфликта source truth). Но этого нет в документации, отчего неосторожное 
    использование булеана и значения которое может быть undefined в качестве значения isOpen, приводит к забавному
    косяку - вьюха должна исчезнуть, но она не исчезает! И тут по-началу грешишь на забагованное API. Но в нем нет бага!

    JaneBurt, 06 Декабря 2021

    Комментарии (26)
  8. Куча / Говнокод #27780

    +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
    (** Set of all possible interleaving of two traces is a trace
      ensemble. As we later prove in [interleaving_to_permutation], this
      definition is dual to [Permutation]. *)
      Inductive Interleaving : list TE -> list TE -> TraceEnsemble :=
      | ilv_cons_l : forall te t1 t2 t,
          Interleaving t1 t2 t ->
          Interleaving (te :: t1) t2 (te :: t)
      | ilv_cons_r : forall te t1 t2 t,
          Interleaving t1 t2 t ->
          Interleaving t1 (te :: t2) (te :: t)
      | ilv_nil : Interleaving [] [] [].
    
    Попытка оптимизации:
    
      (* Left-biased version of [Interleaving] that doesn't make
      distinction between schedulings of commuting elements: *)
      Inductive UniqueInterleaving : list TE -> list TE -> TraceEnsemble :=
      | uilv_cons_l : forall l t1 t2 t,
          UniqueInterleaving t1 t2 t ->
          UniqueInterleaving (l :: t1) t2 (l :: t)
      | uilv_cons_r1 : forall l r t1 t2 t,
          ~trace_elems_commute l r ->
          UniqueInterleaving (l :: t1) t2 (l :: t) ->
          UniqueInterleaving (l :: t1) (r :: t2) (r :: l :: t)
      | uilv_cons_r2 : forall r1 r2 t1 t2 t,
          UniqueInterleaving t1 (r1 :: t2) (r1 :: t) ->
          UniqueInterleaving t1 (r2 :: r1 :: t2) (r2 :: r1 :: t)
      | uilv_nil : forall t, UniqueInterleaving [] t t.

    Сложный говнокод. Почему вторая "оптимизированная" версия работает хуже первой?

    CHayT, 01 Ноября 2021

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

    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
    #define REGISTERS_LIST A, B, C, D, E, SI, BP, SP, IP
    #define LREGISTERS_LIST AH, AL, BH, BL, CH, CL, DH, DL, EH, EL, SIH, SIL, BPH, BPL, SPH, SPL, IPH, IPL
    
    enum RegisterID
    {
    	REGISTERS_LIST,
    	LREGISTERS_LIST
    };
    
    const static std::string registerId2registerName[] = {
    #define _MAP(x) #x
    	MAP_LIST(_MAP, REGISTERS_LIST),
    	MAP_LIST(_MAP, LREGISTERS_LIST)
    };
    #undef _MAP
    
    const static std::map<std::string, RegisterID> registerName2registerId = {
    #define _MAP(x) {#x, x}
    	MAP_LIST(_MAP, REGISTERS_LIST),
    	MAP_LIST(_MAP, LREGISTERS_LIST)
    };
    #undef _MAP

    покруче гомоиконности

    digitalEugene, 30 Октября 2021

    Комментарии (26)
  10. PHP / Говнокод #27702

    −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
    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
    <?php
    
    function is_russian_char($c) {
        return preg_match('/[А-Яа-яЁё]/u', $c);
    }
    
    function nemyxify_char($a) {
        $map = [
            "а" => "a",
            "б" => "6",
            "в" => "B",
            "г" => "r",
            "д" => "g",
            "е" => "e",
            "ё" => "e",
            "ж" => "Jk",
            "з" => "3",
            "и" => "u",
            "й" => "u",
            "к" => "k",
            "л" => "JI",
            "м" => "M",
            "н" => "H",
            "о" => "o",
            "п" => "n",
            "р" => "p",
            "с" => "c",
            "т" => "m",
            "у" => "y",
            "ф" => "qp",
            "х" => "x",
            "ц" => "LL",
            "ч" => "4",
            "ш" => "LLI",
            "щ" => "LLL",
            "ь" => "b",
            "ы" => "bI",
            "ъ" => "b",
            "э" => "3",
            "ю" => "I0",
            "я" => "9I",
    
            "А" => "A",
            "Д" => "D",
            "Е" => "E",
            "Ё" => "E",
            "Ж" => "JK",
            "И" => "U",
            "Й" => "U",
            "К" => "K",
            "О" => "O",
            "Р" => "P",
            "С" => "C",
            "Т" => "T",
            "У" => "Y",
            "Х" => "X",
        ];
        if (isset($map[$a])) {
            return $map[$a];
        }
        return $map[mb_strtolower($a)];
    }
    
    function gk_nemyxify($text) {
        $res = '';
        foreach (preg_split( '//u', $text, null, PREG_SPLIT_NO_EMPTY ) as $c) {
            if (is_russian_char($c)) {
                $res .= nemyxify_char($c);
            } else {
                $res .= $c;
            }
        }
        return $res;
    }

    guest6, 04 Октября 2021

    Комментарии (26)
  11. Куча / Говнокод #27601

    +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
    fn main() {
        println!("Hello World!");
    }
    
    rustc --version --verbose:
    
    rustc 1.52.1 (9bc8c42bb 2021-05-09)
    binary: rustc
    commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
    commit-date: 2021-05-09
    host: powerpc-unknown-linux-gnu
    release: 1.52.1
    LLVM version: 12.0.0
    
    Error output
    
    rustc ./hello.rs
    Illegal instruction (core dumped)

    https://github.com/rust-lang/rust/issues/85238

    Open: clienthax opened this issue on May 12 · 6 comments

    3.14159265, 22 Августа 2021

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