Advertisement
Guest User

r/badcode Challenge 21

a guest
Oct 17th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdarg.h>
  4.  
  5. // Create simple array type.
  6. typedef struct { unsigned length, *values; } array;
  7.  
  8. // Get length of array.
  9. void length(array *arr) {
  10.     unsigned i = 0;
  11.  
  12.     // Keep going until we reach the terminator.
  13.     // This poses a buffer overflow vulnerability.
  14.  
  15.     // The terminator might exist after the data in memory, meaning that
  16.     //  we could potentially overwrite memory after the data.
  17.  
  18.     // Don't rely on this value in a legit program, otherwise you're going
  19.     //  to be in for a bad time.
  20.     for (; arr->values[i] != '\0'; i++);
  21.     arr->length = i;
  22. }
  23.  
  24. // Initialize an array.
  25. void Array(array *arr, ...) {
  26.     va_list vlist;
  27.     va_start(vlist, arr);
  28.  
  29.     // We're not going to free this malloc which will become a
  30.     //  memory leak.
  31.  
  32.     // Even though we're allocating between 8 to 16 KiB of memory
  33.     //  we are unlikely to be using all of it so it's not a problem.
  34.  
  35.     // Unless this is running on a machine with no internal storage and
  36.     //  has less than 8 KiB of RAM left to run this program, then this
  37.     //  shouldn't be an issue.
  38.  
  39.     arr->values = malloc(sizeof(array *) * 2048);
  40.     unsigned i = 0, value = va_arg(vlist, unsigned);
  41.     while (value != 0) {
  42.         arr->values[i] = value;
  43.         i++;
  44.  
  45.         value = va_arg(vlist, unsigned);
  46.     }
  47.  
  48.     // Initialize the length of our array.
  49.     length(arr);
  50.  
  51.     // End off the variable argument list.
  52.     va_end(vlist);
  53. }
  54.  
  55. void find_missing(array *arr) {
  56.     for (unsigned i = 0; i < arr->length; i++) {
  57.  
  58.         // If the next value in the array is not the next character in
  59.         //  the ASCII sequence, then print the missing value and break
  60.         //  free from the loop.
  61.         if (arr->values[i + 1] != arr->values[i] + 1) {
  62.             fprintf(stdout, "%c is missing!\n", arr->values[i] + 1);
  63.             break;
  64.         }
  65.     }
  66. }
  67.  
  68. int main() {
  69.     array array1, array2;
  70.  
  71.     Array(&array1, 'a', 'b', 'd', 'e', 'f', 'g');
  72.     Array(&array2, 'n', 'p');
  73.  
  74.     find_missing(&array1);
  75.     find_missing(&array2);
  76.  
  77.     // This region is used to free memory just before the program gets
  78.     //  terminated. This is useless since that memory will end up getting
  79.     //  freed when the program is terminated.
  80.     free(array1.values);
  81.     free(array2.values);
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement