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

    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 <stdio.h>
    
    int bts(unsigned int n, int bitN) {
        return n & (1 << bitN);
    }
    
    int main() {
        volatile char *p_lacoon;
        unsigned char res = 0;
        
        p_lacoon = ((unsigned )p_lacoon | 01 << 1);
        res |= bts(p_lacoon, 0) | bts(p_lacoon, 1);
    
        p_lacoon = ((unsigned )p_lacoon | 01 & ~!~0);
        p_lacoon = ((unsigned )p_lacoon | 01 << 1 << 0);
        res |= 4 << (bts(p_lacoon, 0) | bts(p_lacoon, 1) ^ 3);
    
        p_lacoon = ((int)p_lacoon | 01 & ~0xFE | 00);
        p_lacoon = ((int)p_lacoon | 01 << 1);
        res |= ~!0xFF << (bts(p_lacoon, 0) | 6 ^ 0x29b);
        res |= ~!0xFC << (bts(p_lacoon, 1) | 4 ^ 0x29c);
        res |= ~!0xF0 << 5;
    
        p_lacoon = (int)p_lacoon >> 01 >> 001 << 001 << 01;
    
        printf("%x, %X %s\n", p_lacoon, (res & 001) ? res-1 : res, "*****. Challenge everything.");
    }

    Предтерминальная стадия мозгфускации.
    Из собеса в подразделение AMD: продемонстрировать варианты хранения при недостатке памяти.
    Работает в GCC, ICC, Clang только -O0.
    Кто хочет разобраться - welcome https://godbolt.org/z/hd4eYeM1Y

    OdduOne, 09 Мая 2025

    Комментарии (3)
  2. Си / Говнокод #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)
  3. Си / Говнокод #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)
  4. Си / Говнокод #29115

    0

    1. 1
    #include <console_io.h>

    конио

    3_dar, 03 Апреля 2025

    Комментарии (5)
  5. Си / Говнокод #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)
  6. Си / Говнокод #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)
  7. Си / Говнокод #29100

    +1

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

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

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

    KPblCA, 08 Марта 2025

    Комментарии (2)
  8. Си / Говнокод #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)
  9. Си / Говнокод #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)
  10. Си / Говнокод #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)