1. Си / Говнокод #29120

    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
    #include <stdio.h>
    #include <stddef.h>
    #include <stdlib.h>
    #include <limits.h>
    
    int gcc_rtm_virt_test(int a, int(*f)(int, int(*)(), float*), float fa[static (-1,0,1)]) {
        short s = sizeof (short) + sizeof 0;
        char b[12] = "0123456789";
    
        for (long ll = sizeof b; ll -= 4;) {
            b[ll]   = '0';
            b[ll-1] = '1';
            b[ll-2] = '2';
            b[ll-3] = '3';
        }
    
        return s - ((1 << 2) - (b[0] - '2'));
    }
    
    int gcc_rtm_cf32(int(*f0)(), int(*f1)()) {
        ptrdiff_t d = f1 - f0;
        // guess you know what you're doing
        if (! (d >> sizeof (ptrdiff_t) * CHAR_BIT - 1)) { 
            while (d--) {
                switch(*(unsigned short*)(f0 + d)) {
                    case 0xc3c9:
                        return 0;
                    case 0xb8:
                        if (!*(unsigned int*)(f0 + d + 1)) {
                            return 1;
                        } 
                        break;
                    case 0xC031:
                        return 2;
                }
            }
            return -1;
        };
    
        return -2;
    }
    
    int main() {
    
        switch (gcc_rtm_cf32(gcc_rtm_virt_test, gcc_rtm_cf32)) {
            case -1:
                fprintf(stderr, "Hey, added smth. special? Maybe -Og? \n"), abort();
                break;
            case 0:
                fprintf(stderr, "Hey, -O0 smells like a dead rat.. check your flags!\n"), abort();
                break;
            case 1:
                fprintf(stderr, "Hey, -O1 is still not allowed.. Would you do better?\n"), abort();
                break;
            case 2:
                fprintf(stdout, "Hey, finally you've captured [-O2 || -O3 || -Ofast || -Os] flag!");
                break;
            default:
                fprintf(stderr, "Hey, looks like you've got impl. defined crap, check PTRDIFF_T stuff.. \n"), abort();
                break;
        };
    
    }

    И далее по тексту..gcc'шники опять обкурились в рантайме. Зачэм так делоть, это прикольно?

    BCHARa, 10 Апреля 2025

    Комментарии (141)
  2. Си / Говнокод #29119

    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
    void rtm_guard(void) {
        /*
            Don't even try to MS VC that stuff, zerobuffy 4eyes.
        */
        unsigned char buf0[0];
        unsigned a = 0xF001BA11;
        unsigned b = 0xF001BA11;
        unsigned c = 0x55550000;
        unsigned d = 0xF001BA11;
        unsigned e = 0xF001BA11;
        unsigned char buf1[0];
        ptrdiff_t diff = buf0 - buf1;
    
        const char* prnt;
        switch (diff) {
            case sizeof rtm_guard:
                prnt = "dear Nizhny Novgorod '-O1' lover";
                break;
            case sizeof (int):
                prnt = "'gcc -O0' bastard";
                break;
            default:
                prnt = "\b";
        };
    
        (diff == sizeof (int) || diff == sizeof rtm_guard) ?
        fprintf(stderr, "%s%s%s", 
                    "Hey you, ", prnt, ", what the hell are you trying to bang here w/o mandatory flags? Forgot something? \n"), abort() : 
        0;
    }

    Сodebomb в рантайме наложил. Но нафига? Присосаться хуком и послать курить джуника с лидом как рукопожато билдить?

    BCHARa, 08 Апреля 2025

    Комментарии (21)
  3. Си / Говнокод #29115

    0

    1. 1
    #include <console_io.h>

    конио

    3_dar, 03 Апреля 2025

    Комментарии (5)
  4. Си / Говнокод #29109

    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
    // https://tio.run/##jY9PC4JAFMTv@ymGJHFjDc06WUEdugpd1YPomgu2in8oED@7WVpRh@jdZt7vDfNCPUwDeeo6oggZpnXEsS6rSGTzZEuIEvFYSI6D42g5QxbHJa8oogwNPIJ@8kLIKtYm06snJwxq7uoD5VO7R1pcEpFyzaDvsP3u@F/YjyzSgzgHQmoUzeP2bgSujw0ag8FkWDBYDEuGVWu/iFneA2rgWv7gjY@Z9FPWox6rml/yuS54VRcShk3arrsB
    
    #include <stdio.h>
    
    #define FOO(p, offset) do { \
        printf("%x\n", &p[-offset]); \
    } while(0)
    
    #define BAR(p, offset) do { \
        printf("%x\n", p[-offset]); \
    } while(0)
    
    
    int main() {
        int a[] = {0, 1, 2, 3, 4, 5};
        int *p = &a[3];
        FOO(p, 1);
        FOO(p, 1u);
        BAR(p, 1);
        BAR(p, 1u);
        return 0;
    }

    testhuest, 27 Марта 2025

    Комментарии (4)
  5. Си / Говнокод #29101

    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
    typedef struct ll_node
    {
      struct ll_node *prev;
      struct ll_node *next;
      int val;
    } ll_node;
    
    ll_node a;
    ll_node b;
    ll_node c = {&a, &b,3};
    
    // не работает
    a.next = &b;
    a.prev = &c;
    
    b.next = &c;
    b.prev = &a;
    
    /*
    c.next = &a;
    c.prev = &b;
    */
    
    // зато так работает:
    ll_node arr[3] = {
      {&arr[2], &arr[1],1},
      {&arr[0], &arr[2],2},
      {&arr[1], &arr[0],3}
    };

    Кольцевой двусвязный список.

    j123123, 10 Марта 2025

    Комментарии (51)
  6. Си / Говнокод #29100

    +1

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

    К нему возвращаются опять и опять, и, что удивительно, тем чаще, чем язык высокоуровневей.

    Напоминает попытки быдла бросить курить.

    KPblCA, 08 Марта 2025

    Комментарии (2)
  7. Си / Говнокод #29098

    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
    // https://godbolt.org/z/f4s13WEWM
    
    #include <inttypes.h>
    
    int test(uint32_t a, uint32_t b)
    {
      if (a > b)
        return a+b;
      return a*b;
    }
    
    int test2(uint32_t a, uint32_t b)
    {
      return (a+b)*(a > b) | (a*b)*!(a > b);
    }
    
    int test3(uint32_t a, uint32_t b)
    {
      return
        ((a+b) & (uint32_t)(!(a > b) - 1)) |
        ((a*b) & (uint32_t)((a > b) - 1));
    }
    
    int test4(uint32_t a, uint32_t b)
    {
      const uint32_t arr[2] = {a+b, a*b};
      return arr[!(a > b)];
    }
    
    /* ASM output
    test:
            bltu    a1, a0, .LBB0_2
            mul     a0, a1, a0
            ret
    .LBB0_2:
            add     a0, a0, a1
            ret
    
    test2:
            bltu    a1, a0, .LBB1_2
            mul     a0, a1, a0
            ret
    .LBB1_2:
            add     a0, a0, a1
            ret
    
    test3:
            bltu    a1, a0, .LBB2_2
            mul     a0, a1, a0
            ret
    .LBB2_2:
            add     a0, a0, a1
            ret
    
    test4:
            addi    sp, sp, -16
            add     a2, a1, a0
            mul     a3, a1, a0
            sltu    a0, a1, a0
            sw      a2, 8(sp)
            sw      a3, 12(sp)
            xori    a0, a0, 1
            slli    a0, a0, 2
            addi    a1, sp, 8
            add     a0, a0, a1
            lw      a0, 0(a0)
            addi    sp, sp, 16
            ret
    */

    Наглядная демонстрация того, что компилятор может насрать на ваши попытки заставить его сгенерить branchless машинный код. Получилось это только в "test4"

    j123123, 07 Марта 2025

    Комментарии (41)
  8. Си / Говнокод #29091

    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
    extern char* strcat( char* dest, const char* src );
    extern int   printf ( const char * restrict format, ... );
    extern int   putchar( int ch );
    
    #define LISPER(n, l, re) ({                                                                                                     \
                    auto int ii = l;                                                                                                          \
                    static unsigned char n[l];                                                                                      \                           
                    while (ii--) {                                                                                                             \
                        *str##n(n, (unsigned char[]){0x09 >> 1 << 1, 0x1911 >> 0xD});                \
                    };                                                                                                                               \
                    *((void**)&re) = printf((unsigned char[]){0x25, 0x73, 0x1917 >> 0xE}, n); \
                    })
    
    struct bombitterLemon{
        union {
            unsigned char   : 0;
            unsigned char   : 0;
            unsigned char v: 7;
        } pacific;
    };
    
    int main(void) {
    
        struct bombitterLemon b0; 
    
        LISPER(cat, 0x40 ^ (2 << 2), b0);
        (*putchar) (*(struct wtf**)&b0);
        LISPER(cat, ((int) (((char*)   (0x8 ^ (1 << 6))) - 3) | 1), b0);
        (**putchar) (*(struct is***)&b0);
        LISPER(cat, ((int) (((short*)  (0x9 << 3))) | 4), b0);
        (***putchar) (*(struct that****)&b0);
        LISPER(cat, ((int) (((int*)    (0x12 << 2))) | 4), b0);
        (**putchar) (*(unsigned char*****)&b0);
        LISPER(cat, ((int) (((long*)   (0x90 >> 1))) | 4 | 2 | 1), b0);
        (*putchar) (b0.pacific.v);
    
    }

    Классический собесный говнокод для кунов на громкие позиции malware ANALyst, security дрист-searcher и даже (о, ужас) compiler devteam в известные шарашки.
    Шланг<15 и ICC жуют с говном не глядя. Чего не сказать о бычаре, дристающего на коврик.
    Осилил, анонимус? Поясни пацанам на пальцах за высер без единого include с ноября прошлого.

    Dulldonch1k, 13 Февраля 2025

    Комментарии (3)
  9. Си / Говнокод #29090

    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
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    void main() {
        ph_fork f1, f2, f3, f4, f5;
    
        f_arr[0] = &f1;
        f_arr[1] = &f2;
        f_arr[2] = &f3;
        f_arr[3] = &f4;
        f_arr[4] = &f5;
    
        philosopher ph1, ph2, ph3, ph4, ph5;
    
        ph_arr[0] = &ph1;
        ph_arr[1] = &ph2;
        ph_arr[2] = &ph3;
        ph_arr[3] = &ph4;
        ph_arr[4] = &ph5;
    
        f1.number = 1;
        sem_init(&f1.is_free, 0, 1);
    
        f2.number = 2;
        sem_init(&f2.is_free, 0, 1);
    
        f3.number = 3;
        sem_init(&f3.is_free, 0, 1);
    
        f4.number = 4;
        sem_init(&f4.is_free, 0, 1);
    
        f5.number = 5;
        sem_init(&f5.is_free, 0, 1);
    
        ph1.number = 1;
        ph1.ph_fork_amount = 0;
        ph1.ph_forks[0] = 1;
        ph1.ph_forks[1] = 2;
        ph1.times_eaten = 0;
        ph1.times_thought = 0;
        ph1.st = THINKING;
        pthread_create(&ph1.thread, NULL, routine, (void*)&ph1);
    
        ph2.number = 2;
        ph2.ph_fork_amount = 0;
        ph2.ph_forks[0] = 2;
        ph2.ph_forks[1] = 3;
        ph2.times_eaten = 0;
        ph2.times_thought = 0;
        ph2.st = THINKING;
        pthread_create(&ph2.thread, NULL, routine, (void*)&ph2);
    
        ph3.number = 3;
        ph3.ph_fork_amount = 0;
        ph3.ph_forks[0] = 3;
        ph3.ph_forks[1] = 4;
        ph3.times_eaten = 0;
        ph3.times_thought = 0;
        ph3.st = THINKING;
        pthread_create(&ph3.thread, NULL, routine, (void*)&ph3);
    
        ph4.number = 4;
        ph4.ph_fork_amount = 0;
        ph4.ph_forks[0] = 4;
        ph4.ph_forks[1] = 5;
        ph4.times_eaten = 0;
        ph4.times_thought = 0;
        ph4.st = THINKING;
        pthread_create(&ph4.thread, NULL, routine, (void*)&ph4);
    
        ph5.number = 5;
        ph5.ph_fork_amount = 0;
        ph5.ph_forks[0] = 5;
        ph5.ph_forks[1] = 1;
        ph5.times_eaten = 0;
        ph5.times_thought = 0;
        ph5.st = THINKING;
        pthread_create(&ph5.thread, NULL, routine, (void*)&ph5);
    
        pthread_join(ph1.thread, NULL);
        pthread_join(ph2.thread, NULL);
        pthread_join(ph3.thread, NULL);
        pthread_join(ph4.thread, NULL);
        pthread_join(ph5.thread, NULL);
    
        sem_destroy(&f1.is_free);
        sem_destroy(&f2.is_free);
        sem_destroy(&f3.is_free);
        sem_destroy(&f4.is_free);
        sem_destroy(&f5.is_free);
    
        printf("\nThe lunch has ended!\n--------\nRESULTS:\nPhilosopher 1 has eaten %d times and thought %d times\nPhilosopher 2 has eaten %d times and thought %d times\nPhilosopher 3 has eaten %d times and thought %d times\nPhilosopher 4 has eaten %d times and thought %d times\nPhilosopher 5 has eaten %d times and thought %d times\n", ph1.times_eaten, ph1.times_thought, ph2.times_eaten, ph2.times_thought, ph3.times_eaten, ph3.times_thought, ph4.times_eaten, ph4.times_thought, ph5.times_eaten, ph5.times_thought);
    }

    Решение задачи про обедающих философов, часть вторая.

    GDMaster, 13 Февраля 2025

    Комментарии (2)
  10. Си / Говнокод #29089

    0

    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
    #include <pthread.h>
    #include <stdio.h>
    #include <semaphore.h>
    #include <unistd.h>
    typedef struct _ph_fork {
        int number;
        sem_t is_free;
    } ph_fork;
    typedef enum state {
        EATING,
        THINKING
    } state;
    typedef struct _philosopher {
        int number;
        int ph_fork_amount;
        int ph_forks[2];
        int times_eaten;
        int times_thought;
        state st;
        pthread_t thread;
    } philosopher;
    
    ph_fork* f_arr[5] = {0};
    philosopher* ph_arr[5] = {0};
    
    void* routine(void* arg) {
        philosopher* ph = (philosopher*)arg;
        int frks[2] = {0};
        frks[0] = ph->ph_forks[0];
        frks[1] = ph->ph_forks[1];
        int ph_fork_left_odd = frks[0] % 2;
        int ph_fork_right_odd = frks[1] % 2;
        printf("Philosopher %d has taken a seat at the table.\n", ph->number);
        for (int i = 0; i < 72; i++) {
            if (!(ph_fork_left_odd && ph_fork_right_odd)) {
                if (ph_fork_left_odd) {
                    sem_wait(&f_arr[frks[0] - 1]->is_free);
                    printf("Philosopher %d takes left fork…\n", ph->number);
                    ph->ph_fork_amount++;
                    sem_wait(&f_arr[frks[1] - 1]->is_free);
                    printf("Philosopher %d takes right fork…\n", ph->number);
                    ph->ph_fork_amount++;
                }
                if (ph_fork_right_odd) {
                    sem_wait(&f_arr[frks[1] - 1]->is_free);
                    printf("Philosopher %d takes right fork…\n", ph->number);
                    ph->ph_fork_amount++;
                    sem_wait(&f_arr[frks[0] - 1]->is_free);
                    printf("Philosopher %d takes left fork…\n", ph->number);
                    ph->ph_fork_amount++;
                }
            } else { // Special case when both ph_forks are odd
                sem_wait(&f_arr[frks[0] - 1]->is_free);
                printf("Philosopher %d takes left fork…\n", ph->number);
                ph->ph_fork_amount++;
                sem_wait(&f_arr[frks[1] - 1]->is_free);
                printf("Philosopher %d takes right fork…\n", ph->number);
                ph->ph_fork_amount++;
            }
            if (ph->ph_fork_amount == 2) {
                ph->st = EATING;
                ph->times_eaten++;
                printf("Philosopher %d is eating…\n", ph->number);
                sleep(1);
            };
            if (!(ph_fork_left_odd && ph_fork_right_odd)) {
                if (ph_fork_left_odd) {
                    printf("Philosopher %d puts back left fork…\n", ph->number);
                    sem_post(&f_arr[frks[0] - 1]->is_free);
                    ph->ph_fork_amount--;
                    printf("Philosopher %d puts back right fork…\n", ph->number);
                    sem_post(&f_arr[frks[1] - 1]->is_free);
                    ph->ph_fork_amount--;
                }
                if (ph_fork_right_odd) {
                    printf("Philosopher %d puts back right fork…\n", ph->number);
                    sem_post(&f_arr[frks[1] - 1]->is_free);
                    ph->ph_fork_amount--;
                    printf("Philosopher %d puts back left fork…\n", ph->number);
                    sem_post(&f_arr[frks[0] - 1]->is_free);
                    ph->ph_fork_amount--;
                }
            } else { // Special case when both ph_forks are odd
                printf("Philosopher %d puts back left fork…\n", ph->number);
                sem_post(&f_arr[frks[0] - 1]->is_free);
                ph->ph_fork_amount--;
                printf("Philosopher %d puts back right fork…\n", ph->number);
                sem_post(&f_arr[frks[1] - 1]->is_free);
                ph->ph_fork_amount--;
            }
            if (ph->ph_fork_amount == 0) {
                ph->st = THINKING;
                printf("Philosopher %d is thinking…\n", ph->number);
                ph->times_thought++;
                sleep(1);
            }
        }
        printf("Philosopher %d has finished his lunch and left.\n", ph->number);
        return NULL;
    };

    Решение задачи про обедающих философов, часть первая.

    GDMaster, 13 Февраля 2025

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