Changeset 544 for trunk/src/slib.c
- Timestamp:
- Feb 17, 2019, 2:41:49 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/slib.c
r539 r544 588 588 /* 589 589 * Have memset in a different translation unit (i.e. this) to prevent 590 * it to get optimized away 590 * it to get optimized away ...not safe with link-time optimisation... 591 591 */ 592 void *sl_memset(void *s, int c, size_t n) 593 { 594 return memset(s, c,n); 592 void * sl_memset(void *s, int c, size_t n) 593 { 594 /* See: 595 * https://www.usenix.org/sites/default/files/conference/protected-files/usenixsecurity17_slides_zhaomo_yang.pdf 596 */ 597 #if defined(HAVE_EXPLICIT_MEMSET) 598 return explicit_memset(s, c, n); 599 #elif defined(HAVE_EXPLICIT_BZERO) 600 if (c == 0) { 601 explicit_bzero(s, n); 602 return s; 603 } else { 604 return memset(s, c, n); 605 } 606 #elif defined(__GNUC__) 607 memset(s, c, n); 608 __asm__ __volatile__ ("" ::"r"(s): "memory"); /* compiler barrier */ 609 return s; 610 #else 611 if (c == 0) { 612 size_t i; 613 volatile unsigned char * t_s = (volatile unsigned char *)s; 614 for (i=0; i<n; ++i) 615 t_s[i] = 0; 616 return s; 617 } else { 618 return memset(s, c, n); 619 } 620 #endif 595 621 } 596 622 … … 1071 1097 if (a != NULL && b != NULL) 1072 1098 return (strcmp(a, b)); 1099 else if (a == NULL && b != NULL) 1100 return (-1); 1101 else if (a != NULL && b == NULL) 1102 return (1); 1103 else 1104 return (-7); /* default to not equal */ 1105 } 1106 1107 /* Does not report sign. */ 1108 int sl_ts_strncmp(const char * a, const char * b, size_t n) 1109 { 1110 #ifdef SL_FAIL_ON_ERROR 1111 SL_REQUIRE (a != NULL, _("a != NULL")); 1112 SL_REQUIRE (b != NULL, _("b != NULL")); 1113 SL_REQUIRE (n > 0, _("n > 0")); 1114 #endif 1115 1116 if (a != NULL && b != NULL) 1117 { 1118 const unsigned char *a1 = (const unsigned char *)a; 1119 const unsigned char *b1 = (const unsigned char *)b; 1120 size_t i; 1121 int retval=0; 1122 /* The simple index based access is optimized best by the 1123 * compiler (tested with gcc 7.3.0). */ 1124 for (i = 0; i < n; ++i) 1125 { 1126 if (a1[i] == '\0' || b1[i] == '\0') 1127 break; 1128 retval |= (a1[i] ^ b1[i]); 1129 } 1130 /* if (retval == 0) --> false (0) */ 1131 return (retval != 0); 1132 } 1073 1133 else if (a == NULL && b != NULL) 1074 1134 return (-1);
Note:
See TracChangeset
for help on using the changeset viewer.