gavinhoward / bc

An implementation of the POSIX bc calculator with GNU extensions and dc, moved away from GitHub. Finished, but well-maintained.
https://git.gavinhoward.com/gavin/bc
Other
145 stars 29 forks source link

mac build broken by #define of strcpy (conflicts with Apple's fortify implementation) #35

Closed enh-google closed 2 years ago

enh-google commented 2 years ago

seems like the attempt to use strcpy_s() on Windows broke the macOS build. it conflicts with the mac's _FORTIFY_SOURCE implementation (which [TIL] appears to be on by default), which has a #define of strcpy to __builtin_strcpy_chk.

here's the easiest option... i went with a new name because i assume you do want fortify where available rather than, say, strncpy() or strlcpy() --- certainly fortify seems closest to strcpy_s():

diff --git a/include/vector.h b/include/vector.h
index 8f7cbbcc..948e4751 100644
--- a/include/vector.h
+++ b/include/vector.h
@@ -441,7 +441,7 @@ void bc_slabvec_print(BcVec *v, const char *func);
  *           contain @a s.
  * @param s  The source string.
  */
-#define strcpy(d, l, s) strcpy(d, s)
+#define lstrcpy(d, l, s) strcpy(d, s)

 #else // _WIN32

@@ -452,7 +452,7 @@ void bc_slabvec_print(BcVec *v, const char *func);
  *           contain @a s.
  * @param s  The source string.
  */
-#define strcpy(d, l, s) strcpy_s(d, l, s)
+#define lstrcpy(d, l, s) strcpy_s(d, l, s)

 #endif // _WIN32

diff --git a/src/vector.c b/src/vector.c
index 1cd90f72..022a8548 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -461,7 +461,7 @@ static char* bc_slab_add(BcSlab *s, const char *str, size_t len) {

        ptr = (char*) (s->s + s->len);

-       strcpy(ptr, len, str);
+       lstrcpy(ptr, len, str);

        s->len += len;

(we don't have mac presubmit, so i didn't notice this until i submitted the upgrade this morning.)

gavinhoward commented 2 years ago

I have tweaked your fix, tested it, and it works. So I've released 5.0.1, and you should be good to go.

Please reopen if something go wrong.

gavinhoward commented 2 years ago

Oh, yeah, forgot to say: FreeBSD didn't tell me about any more problems and seem to have solved it on their end.