String stuff that should be in stdlib but isn't.
Dependents: X10Svr SSDP_Server
Revision 3:bc30d348f5b4, committed 2019-02-27
- Comitter:
- WiredHome
- Date:
- Wed Feb 27 18:22:04 2019 +0000
- Parent:
- 2:c7a3039893cb
- Commit message:
- Fix the venerable off-by-one error; if the two strings are adjacent it was detecting a false overlap.
Changed in this revision
SW_String.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r c7a3039893cb -r bc30d348f5b4 SW_String.cpp --- a/SW_String.cpp Sun Nov 18 04:04:48 2018 +0000 +++ b/SW_String.cpp Wed Feb 27 18:22:04 2019 +0000 @@ -43,16 +43,22 @@ } if (src == NULL || *src == '\0') return 0; // done, that was easy. - if (src >= dst && src <= dst + dstSize) { + // [Source .... + // [Destination ... ] + if (src >= dst && src < (dst + dstSize)) { ERR("strcat_s FAIL source overlaps destination"); return -3; } int dstLen = strlen(dst); int srcLen = strlen(src); + // [Source ..... +srcLen] + // [Destination + dstSize] if (src + srcLen >= dst && src + srcLen <= dst + dstSize) { ERR("strcat_s FAIL source + length overlaps destination"); return -3; } + // [Source length > freespace] + // [Destination ... \0[freespace]] if (dstLen + srcLen > dstSize) { ERR("strcat_s FAIL dstLen + srcLen > size"); return -2; @@ -91,7 +97,7 @@ *dst = '\0'; return 0; // done, that was easy. } - if (src >= dst && src <= dst + dstSize) { + if (src >= dst && src < dst + dstSize) { ERR("strcpy_s FAIL source overlaps destination"); return -3; }