ObjFW  Check-in [0b962df002]

Overview
Comment:OFString_UTF8: Improved range checks
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0b962df002af925d65cfa70859a3df6e8c428e66d36d31cdc7312ca26e70db2d
User & Date: js on 2019-04-22 16:53:51
Other Links: manifest | tags
Context
2019-04-23
00:12
OFSystemInfo: Add +[CPUModel] check-in: 2447bb2967 user: js tags: trunk
2019-04-22
16:53
OFString_UTF8: Improved range checks check-in: 0b962df002 user: js tags: trunk
09:17
OFSystemInfo: AltiVec detection for AmigaOS 4 check-in: 7ef650a6ec user: js tags: trunk
Changes

Modified src/OFMutableString_UTF8.m from [afad633464] to [8884971f0c].

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
	size_t lenNew;
	ssize_t lenOld;

	if (_s->isUTF8)
		idx = of_string_utf8_get_position(_s->cString, idx,
		    _s->cStringLength);

	if (idx > _s->cStringLength)
		@throw [OFOutOfRangeException exception];

	/* Shortcut if old and new character both are ASCII */
	if (character < 0x80 && !(_s->cString[idx] & 0x80)) {
		_s->hashed = false;
		_s->cString[idx] = character;
		return;







|







203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
	size_t lenNew;
	ssize_t lenOld;

	if (_s->isUTF8)
		idx = of_string_utf8_get_position(_s->cString, idx,
		    _s->cStringLength);

	if (idx >= _s->cStringLength)
		@throw [OFOutOfRangeException exception];

	/* Shortcut if old and new character both are ASCII */
	if (character < 0x80 && !(_s->cString[idx] & 0x80)) {
		_s->hashed = false;
		_s->cString[idx] = character;
		return;

Modified src/OFString.m from [fd85923ed1] to [8cbfa26fc2].

261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

	return 0;
}

ssize_t
of_string_utf8_decode(const char *buffer_, size_t length, of_unichar_t *ret)
{
	const uint8_t *buffer = (const uint8_t *)buffer_;

	if (!(*buffer & 0x80)) {
		*ret = buffer[0];
		return 1;
	}

	if ((*buffer & 0xE0) == 0xC0) {







|







261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

	return 0;
}

ssize_t
of_string_utf8_decode(const char *buffer_, size_t length, of_unichar_t *ret)
{
	const unsigned char *buffer = (const unsigned char *)buffer_;

	if (!(*buffer & 0x80)) {
		*ret = buffer[0];
		return 1;
	}

	if ((*buffer & 0xE0) == 0xC0) {

Modified src/OFString_UTF8.m from [b5bfc583a1] to [c33b1359c4].

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

size_t
of_string_utf8_get_position(const char *string, size_t idx, size_t length)
{
	for (size_t i = 0; i <= idx; i++)
		if OF_UNLIKELY ((string[i] & 0xC0) == 0x80)
			if (++idx > length)
				return OF_NOT_FOUND;

	return idx;
}

@implementation OFString_UTF8
- (instancetype)init
{







|







163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

size_t
of_string_utf8_get_position(const char *string, size_t idx, size_t length)
{
	for (size_t i = 0; i <= idx; i++)
		if OF_UNLIKELY ((string[i] & 0xC0) == 0x80)
			if (++idx > length)
				@throw [OFInvalidFormatException exception];

	return idx;
}

@implementation OFString_UTF8
- (instancetype)init
{

Modified src/macros.h from [6558af17e7] to [989f46865e].

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# define OF_LIKELY(cond) (__builtin_expect(!!(cond), 1))
# define OF_UNLIKELY(cond) (__builtin_expect(!!(cond), 0))
# define OF_CONST_FUNC __attribute__((__const__))
# define OF_NO_RETURN_FUNC __attribute__((__noreturn__))
# define OF_WEAK_REF(sym) __attribute__((__weakref__(sym)))
#else
# define OF_INLINE inline
# define OF_LIKELY(cond) cond
# define OF_UNLIKELY(cond) cond
# define OF_CONST_FUNC
# define OF_NO_RETURN_FUNC
# define OF_WEAK_REF(sym)
#endif

#ifdef OF_BIG_ENDIAN
# define OF_BYTE_ORDER_NATIVE OF_BYTE_ORDER_BIG_ENDIAN







|
|







89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# define OF_LIKELY(cond) (__builtin_expect(!!(cond), 1))
# define OF_UNLIKELY(cond) (__builtin_expect(!!(cond), 0))
# define OF_CONST_FUNC __attribute__((__const__))
# define OF_NO_RETURN_FUNC __attribute__((__noreturn__))
# define OF_WEAK_REF(sym) __attribute__((__weakref__(sym)))
#else
# define OF_INLINE inline
# define OF_LIKELY(cond) (cond)
# define OF_UNLIKELY(cond) (cond)
# define OF_CONST_FUNC
# define OF_NO_RETURN_FUNC
# define OF_WEAK_REF(sym)
#endif

#ifdef OF_BIG_ENDIAN
# define OF_BYTE_ORDER_NATIVE OF_BYTE_ORDER_BIG_ENDIAN