Last active
January 28, 2023 11:54
-
-
Save theteachr/9fdbdf52202b828c705fb32084d8ad76 to your computer and use it in GitHub Desktop.
Destructive XOR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#define MAX_CHARS 64 | |
void swap(char* a, char* b) { | |
*a = *a ^ *b; | |
*b = *a ^ *b; | |
*a = *a ^ *b; | |
} | |
void rev(char* text, size_t size) { | |
for ( | |
size_t left = 0, right = size - 1; | |
left <= right; | |
left++, right-- | |
) { | |
swap(text + left, text + right); | |
} | |
} | |
int main(int argc, char* argv[]) { | |
if (argc < 2) { | |
fprintf(stderr, "No input param given :o\n"); | |
exit(EXIT_FAILURE); | |
} | |
char text[MAX_CHARS]; | |
strncpy(text, argv[1], MAX_CHARS); | |
rev(text, strlen(text)); | |
printf("Given: %s\n", argv[1]); | |
printf("Revved: %s\n", text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At function
rev
line 15:value of variable
right
will underflow whensize
is 0.At function
main
line 32:result of
strlen
call may be undefined since the'\0'
character maybe was not copied bystrncpy
.