Skip to content

Instantly share code, notes, and snippets.

@moratorium08
Created December 23, 2023 05:41
Show Gist options
  • Save moratorium08/ab48b95807bebc2fe3e41648173ebaed to your computer and use it in GitHub Desktop.
Save moratorium08/ab48b95807bebc2fe3e41648173ebaed to your computer and use it in GitHub Desktop.
ubuntu@ip-172-31-28-212:~/seccon23_finals/DataStore2$ diff old.c main.c
32a33
> uint8_t ref;
40a42
> static int duplicate_recursive(data_t *dst, data_t *src);
93c95
< if(count > 0x10){
---
> if(count > 0x8){
134a137
> str->ref = 1;
163c166
< if(idx > arr->count)
---
> if(idx >= arr->count)
168a172
> "3. Copy\n"
177a182,191
> case 3:
> {
> printf("dest index: ");
> unsigned dst_idx = getint();
> if(dst_idx >= arr->count)
> return -1;
>
> duplicate_recursive(&arr->data[dst_idx], &arr->data[idx]);
> }
> break;
182,187d195
< {
< str_t *str = data->p_str;
< printf("new string (max:%ld bytes): ", str->size);
< getnline(str->content, str->size+1);
< }
< break;
210a219
> data->p_arr = NULL;
216,217c225,229
< free(str->content);
< free(str);
---
> if(--str->ref < 1){
> free(str->content);
> free(str);
> }
> data->p_str = NULL;
221a234,270
>
> return 0;
> }
>
> static int duplicate_recursive(data_t *dst, data_t *src){
> if(!src || !dst || dst->type != TYPE_EMPTY)
> return -1;
>
> switch(src->type){
> case TYPE_ARRAY:
> {
> arr_t *arr = src->p_arr;
> size_t sz = sizeof(arr_t)+sizeof(data_t)*arr->count;
> arr_t *new = (arr_t*)malloc(sz);
> if(!new)
> return -1;
> memcpy(new, arr, sz);
>
> for(int i=0; i<arr->count; i++){
> switch(arr->data[i].type){
> case TYPE_ARRAY:
> case TYPE_STRING:
> new->data[i].type = TYPE_EMPTY;
> if(duplicate_recursive(&new->data[i], &arr->data[i]))
> return -1;
> }
> }
>
> dst->type = TYPE_ARRAY;
> dst->p_arr = new;
> }
> break;
> case TYPE_STRING:
> src->p_str->ref++;
> default:
> memcpy(dst, src, sizeof(data_t));
> }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment