mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
CHK_INDEX macro
This commit is contained in:
parent
d8919bf1e8
commit
e10f4228c0
@ -22,7 +22,7 @@ void String_delete(String s) {
|
||||
}
|
||||
|
||||
void String_string_MINUS_set_BANG_(String *s, int i, char ch) {
|
||||
assert((size_t)i < strlen(*s));
|
||||
CHK_INDEX(i, strlen(*s));
|
||||
(*s)[i] = ch;
|
||||
}
|
||||
|
||||
@ -58,7 +58,7 @@ void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i, const String *s
|
||||
*
|
||||
* so this write is safe
|
||||
*/
|
||||
assert((size_t)(i+lsrc) < strlen(*into)+1);
|
||||
CHK_INDEX(i+lsrc, strlen(*into)+1);
|
||||
strncpy(dest, *src, lsrc);
|
||||
}
|
||||
|
||||
|
17
core/core.h
17
core/core.h
@ -2,8 +2,8 @@
|
||||
#define PRELUDE_H
|
||||
|
||||
#include <assert.h>
|
||||
#include "carp_stdbool.h"
|
||||
#include <stddef.h>
|
||||
#include "carp_stdbool.h"
|
||||
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
|
||||
#include <windows.h>
|
||||
#endif
|
||||
@ -11,6 +11,21 @@
|
||||
typedef char* String;
|
||||
typedef char* Pattern;
|
||||
|
||||
#if defined NDEBUG
|
||||
#define CHK_INDEX(i,n)
|
||||
#else
|
||||
#define CHK_INDEX(i,n) \
|
||||
do { \
|
||||
size_t __si = (size_t)i; \
|
||||
size_t __ni = (size_t)n; \
|
||||
if (!(__si < __ni)) { \
|
||||
printf(__FILE__ ":%u: bad index: %zd < %zd\n", \
|
||||
__LINE__, (ssize_t)i, (ssize_t)n); \
|
||||
abort(); \
|
||||
} \
|
||||
}while (0)
|
||||
#endif
|
||||
|
||||
// Array
|
||||
typedef struct {
|
||||
size_t len;
|
||||
|
Loading…
Reference in New Issue
Block a user