Page 1 of 1

error and omission in file open_memstream.c cause of build errors

Posted: Wed Sep 23, 2020 12:29 pm
by paulskpt
Hello all,
After a clean installation of Ubuntu 20.04 and a re-installation of Espressif esp-idf I tried to build the example project 'Hello-World'.
It resulted in build errors.

System used: Linux Ubuntu 20.04
Espressif esp-idf installed in ~/esp.
Version: v4.3-dev-1197-g8bc19ba89-dirty
Development board: Espressif ESP32-S2 Soala 1R (WROVER)

To make a long story short. I managed to solve the problem by editing the offending file:
~/esp/esp-idf/components/cbor/tinycbor/src/open_memstream.c

These are the changes / additions i did:

1st error: in line 46: .... # error ..."Cannot implement open..."
a) adding starting at line #31:
#ifndef __linux__
#define __linux__ 1
#endif

b) by changing (the former) line 45 #error...
#else
#error "Cannot implement open_memstream"
#endif

into:
#else
const char *error="Cannot implement open_memstream";
#endif

After these alterations the build process resulted in no error.

See the contents of the edited file below:
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2015 Intel Corporation
  4. **
  5. ** Permission is hereby granted, free of charge, to any person obtaining a copy
  6. ** of this software and associated documentation files (the "Software"), to deal
  7. ** in the Software without restriction, including without limitation the rights
  8. ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. ** copies of the Software, and to permit persons to whom the Software is
  10. ** furnished to do so, subject to the following conditions:
  11. **
  12. ** The above copyright notice and this permission notice shall be included in
  13. ** all copies or substantial portions of the Software.
  14. **
  15. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. ** THE SOFTWARE.
  22. **
  23. ****************************************************************************/
  24.  
  25. #define _BSD_SOURCE 1
  26. #define _DEFAULT_SOURCE 1
  27. #define _GNU_SOURCE 1
  28.  
  29. // Addition Paulsk (for test) because of (probably) missing define __linux__
  30. // because compiler gives errors: unknown type RetType and LenType
  31. #ifndef __linux__
  32. #define __linux__ 1
  33. #endif
  34.  
  35.  
  36. #include <sys/types.h>
  37. #include <errno.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41.  
  42. #if defined(__unix__) || defined(__APPLE__)
  43. #include <unistd.h>
  44. #endif
  45.  
  46. #ifdef __APPLE__
  47. typedef int RetType;
  48. typedef int LenType;
  49. #elif __linux__
  50. typedef ssize_t RetType;
  51. typedef size_t LenType;
  52. #else
  53. const char *error="Cannot implement open_memstream!"; // Changed by paulsk 2020-09-23 because of error "#error Cannot ..." (no type def, no '=' and without ; at end of line)
  54. #endif
  55.  
  56. #include "compilersupport_p.h"
  57.  
  58. struct Buffer
  59. {
  60.     char **ptr;
  61.     size_t *len;
  62.     size_t alloc;
  63. };
  64.  
  65. static RetType write_to_buffer(void *cookie, const char *data, LenType len)
  66. {
  67.     struct Buffer *b = (struct Buffer *)cookie;
  68.     char *ptr = *b->ptr;
  69.     size_t newsize;
  70.  
  71.     errno = EFBIG;
  72.     if (unlikely(add_check_overflow(*b->len, len, &newsize)))
  73.         return -1;
  74.  
  75.     if (newsize >= b->alloc) { // NB! one extra byte is needed to avoid buffer overflow at close_buffer
  76.         // make room
  77.         size_t newalloc = newsize + newsize / 2 + 1;    // give 50% more room
  78.         ptr = realloc(ptr, newalloc);
  79.         if (ptr == NULL)
  80.             return -1;
  81.         b->alloc = newalloc;
  82.         *b->ptr = ptr;
  83.     }
  84.  
  85.     memcpy(ptr + *b->len, data, len);
  86.     *b->len = newsize;
  87.     return len;
  88. }
  89.  
  90. static int close_buffer(void *cookie)
  91. {
  92.     struct Buffer *b = (struct Buffer *)cookie;
  93.     if (*b->ptr)
  94.         (*b->ptr)[*b->len] = '\0';
  95.     free(b);
  96.     return 0;
  97. }
  98.  
  99. FILE *open_memstream(char **bufptr, size_t *lenptr)
  100. {
  101.     struct Buffer *b = (struct Buffer *)malloc(sizeof(struct Buffer));
  102.     if (b == NULL)
  103.         return NULL;
  104.     b->alloc = 0;
  105.     b->len = lenptr;
  106.     b->ptr = bufptr;
  107.     *bufptr = NULL;
  108.     *lenptr = 0;
  109.  
  110. #ifdef __APPLE__
  111.     return funopen(b, NULL, write_to_buffer, NULL, close_buffer);
  112. #elif __linux__
  113.     static const cookie_io_functions_t vtable = {
  114.         NULL,
  115.         write_to_buffer,
  116.         NULL,
  117.         close_buffer
  118.     };
  119.     return fopencookie(b, "w", vtable);
  120. #endif
  121. }