ObjFW  Check-in [535c2d5d9b]

Overview
Comment:Make using -[tryReadLine] + OFStreamObserver safe.

This makes it impossible for the developer to accidentally create a DoS.
When data has been received after calling -[tryReadLine] and the string
is still incomplete, -[streamIsReadyForReading:] will not be called
anymore until new data has been received, thus preventing spinning in a
loop.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 535c2d5d9bc4d7c65853b74c4402568415eeeedb840b47be7c1a8198906f091f
User & Date: js on 2011-09-19 16:22:23
Other Links: manifest | tags
Context
2011-09-19
16:34
Rename -[allocMemoryForNItems:withSize:] and friends.
It is now -[allocMemoryForNItems:ofSize:].
check-in: f173477bef user: js tags: trunk
16:22
Make using -[tryReadLine] + OFStreamObserver safe. check-in: 535c2d5d9b user: js tags: trunk
13:07
Rename -[bindToPort:onHost:] to -[bindToHost:port:].
This way it's consistent with -[connectToHost:port].
check-in: b8517c63ca user: js tags: trunk
Changes

Modified src/OFStream.h from [d46e5522c2] to [3697389637].

33
34
35
36
37
38
39

40
41
42
43
44
45
46
@interface OFStream: OFObject
{
	char   *cache;
	char   *writeBuffer;
	size_t cacheLength, writeBufferLength;
	BOOL   buffersWrites;
	BOOL   blocking;

}

#ifdef OF_HAVE_PROPERTIES
@property (assign, getter=isBlocking) BOOL blocking;
#endif

/**







>







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@interface OFStream: OFObject
{
	char   *cache;
	char   *writeBuffer;
	size_t cacheLength, writeBufferLength;
	BOOL   buffersWrites;
	BOOL   blocking;
	BOOL   waitingForDelimiter;
}

#ifdef OF_HAVE_PROPERTIES
@property (assign, getter=isBlocking) BOOL blocking;
#endif

/**
799
800
801
802
803
804
805




806
 */
- (int)fileDescriptor;

/**
 * \brief Closes the stream.
 */
- (void)close;




@end







>
>
>
>

800
801
802
803
804
805
806
807
808
809
810
811
 */
- (int)fileDescriptor;

/**
 * \brief Closes the stream.
 */
- (void)close;

/// \cond internal
- (BOOL)_isWaitingForDelimiter;
/// \endcond
@end

Modified src/OFStream.m from [69277f1386] to [d011dc479a].

503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
	} @finally {
		[self freeMemory: buffer];
	}

	return ret;
}

- (OFString*)_tryReadLineWithEncoding: (of_string_encoding_t)encoding
			   checkCache: (BOOL)checkCache
{
	size_t i, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* Look if there's a line or \0 in our cache */
	if (checkCache && cache != NULL) {
		for (i = 0; i < cacheLength; i++) {
			if (OF_UNLIKELY(cache[i] == '\n' ||
			    cache[i] == '\0')) {
				retLength = i;

				if (i > 0 && cache[i - 1] == '\r')
					retLength--;







|
<






|







503
504
505
506
507
508
509
510

511
512
513
514
515
516
517
518
519
520
521
522
523
524
	} @finally {
		[self freeMemory: buffer];
	}

	return ret;
}

- (OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding

{
	size_t i, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* Look if there's a line or \0 in our cache */
	if (!waitingForDelimiter && cache != NULL) {
		for (i = 0; i < cacheLength; i++) {
			if (OF_UNLIKELY(cache[i] == '\n' ||
			    cache[i] == '\0')) {
				retLength = i;

				if (i > 0 && cache[i - 1] == '\r')
					retLength--;
534
535
536
537
538
539
540

541
542
543
544
545
546
547
548
549
550
551

552

553
554
555
556
557
558
559
560
561
562
563
564
565
566

567
568
569
570
571
572
573
					memcpy(newCache, cache + i + 1,
					    cacheLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength -= i + 1;


				return ret;
			}
		}
	}

	/* Read and see if we get a newline or \0 */
	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		if ([self _isAtEndOfStream]) {
			if (cache == NULL)

				return nil;


			retLength = cacheLength;

			if (retLength > 0 && cache[retLength - 1] == '\r')
				retLength--;

			ret = [OFString stringWithCString: cache
						 encoding: encoding
						   length: retLength];

			[self freeMemory: cache];
			cache = NULL;
			cacheLength = 0;


			return ret;
		}

		bufferLength = [self _readNBytes: of_pagesize
				      intoBuffer: buffer];

		/* Look if there's a newline or \0 */







>










|
>

>














>







533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
					memcpy(newCache, cache + i + 1,
					    cacheLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength -= i + 1;

				waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we get a newline or \0 */
	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		if ([self _isAtEndOfStream]) {
			if (cache == NULL) {
				waitingForDelimiter = NO;
				return nil;
			}

			retLength = cacheLength;

			if (retLength > 0 && cache[retLength - 1] == '\r')
				retLength--;

			ret = [OFString stringWithCString: cache
						 encoding: encoding
						   length: retLength];

			[self freeMemory: cache];
			cache = NULL;
			cacheLength = 0;

			waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self _readNBytes: of_pagesize
				      intoBuffer: buffer];

		/* Look if there's a newline or \0 */
621
622
623
624
625
626
627

628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647

648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
					memcpy(newCache, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength = bufferLength - i - 1;


				return ret;
			}
		}

		/* There was no newline or \0 */
		cache = [self resizeMemory: cache
				    toSize: cacheLength + bufferLength];

		/*
		 * It's possible that cacheLen + len is 0 and thus cache was
		 * set to NULL by resizeMemory:toSize:.
		 */
		if (cache != NULL)
			memcpy(cache + cacheLength, buffer, bufferLength);

		cacheLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}


	return nil;
}

- (OFString*)readLine
{
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readLineWithEncoding: (of_string_encoding_t)encoding
{
	OFString *line = nil;

	if ((line = [self _tryReadLineWithEncoding: encoding
					checkCache: YES]) != nil)
		return line;

	while ((line = [self _tryReadLineWithEncoding: encoding
					   checkCache: NO]) == nil)
		if ([self isAtEndOfStream])
			return nil;

	return line;
}

- (OFString*)tryReadLine
{
	return [self tryReadLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding
{
	return [self _tryReadLineWithEncoding: encoding
				   checkCache: YES];
}

- (OFString*)_tryReadTillDelimiter: (OFString*)delimiter
		      withEncoding: (of_string_encoding_t)encoding
			checkCache: (BOOL)checkCache
{
	const char *delimiterUTF8String;
	size_t i, j, delimiterLength, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* FIXME: Convert delimiter to specified charset */
	delimiterUTF8String = [delimiter UTF8String];
	delimiterLength = [delimiter UTF8StringLength];
	j = 0;

	if (delimiterLength == 0)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	/* Look if there's something in our cache */
	if (checkCache && cache != NULL) {
		for (i = 0; i < cacheLength; i++) {
			if (cache[i] != delimiterUTF8String[j++])
				j = 0;

			if (j == delimiterLength || cache[i] == '\0') {
				if (cache[i] == '\0')
					delimiterLength = 1;







>




















>












|
<
<
<
<
<











<
<
<
<
<
<
|
|
<
















|







624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665





666
667
668
669
670
671
672
673
674
675
676






677
678

679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
					memcpy(newCache, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength = bufferLength - i - 1;

				waitingForDelimiter = NO;
				return ret;
			}
		}

		/* There was no newline or \0 */
		cache = [self resizeMemory: cache
				    toSize: cacheLength + bufferLength];

		/*
		 * It's possible that cacheLen + len is 0 and thus cache was
		 * set to NULL by resizeMemory:toSize:.
		 */
		if (cache != NULL)
			memcpy(cache + cacheLength, buffer, bufferLength);

		cacheLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	waitingForDelimiter = YES;
	return nil;
}

- (OFString*)readLine
{
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readLineWithEncoding: (of_string_encoding_t)encoding
{
	OFString *line = nil;

	while ((line = [self tryReadLineWithEncoding: encoding]) == nil)





		if ([self isAtEndOfStream])
			return nil;

	return line;
}

- (OFString*)tryReadLine
{
	return [self tryReadLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}







- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
		     withEncoding: (of_string_encoding_t)encoding

{
	const char *delimiterUTF8String;
	size_t i, j, delimiterLength, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* FIXME: Convert delimiter to specified charset */
	delimiterUTF8String = [delimiter UTF8String];
	delimiterLength = [delimiter UTF8StringLength];
	j = 0;

	if (delimiterLength == 0)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	/* Look if there's something in our cache */
	if (!waitingForDelimiter && cache != NULL) {
		for (i = 0; i < cacheLength; i++) {
			if (cache[i] != delimiterUTF8String[j++])
				j = 0;

			if (j == delimiterLength || cache[i] == '\0') {
				if (cache[i] == '\0')
					delimiterLength = 1;
719
720
721
722
723
724
725

726
727
728
729
730
731
732
733
734
735
736

737

738
739
740
741
742
743
744
745
746

747
748
749
750
751
752
753
					memcpy(newCache, cache + i + 1,
					    cacheLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength -= i + 1;


				return ret;
			}
		}
	}

	/* Read and see if we get a delimiter or \0 */
	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		if ([self _isAtEndOfStream]) {
			if (cache == NULL)

				return nil;


			ret = [OFString stringWithCString: cache
						 encoding: encoding
						   length: cacheLength];

			[self freeMemory: cache];
			cache = NULL;
			cacheLength = 0;


			return ret;
		}

		bufferLength = [self _readNBytes: of_pagesize
				      intoBuffer: buffer];

		/* Look if there's a delimiter or \0 */







>










|
>

>









>







712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
					memcpy(newCache, cache + i + 1,
					    cacheLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength -= i + 1;

				waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we get a delimiter or \0 */
	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		if ([self _isAtEndOfStream]) {
			if (cache == NULL) {
				waitingForDelimiter = NO;
				return nil;
			}

			ret = [OFString stringWithCString: cache
						 encoding: encoding
						   length: cacheLength];

			[self freeMemory: cache];
			cache = NULL;
			cacheLength = 0;

			waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self _readNBytes: of_pagesize
				      intoBuffer: buffer];

		/* Look if there's a delimiter or \0 */
790
791
792
793
794
795
796

797
798
799
800
801
802
803
					memcpy(newCache, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength = bufferLength - i - 1;


				return ret;
			}
		}

		/* Neither the delimiter nor \0 was found */
		cache = [self resizeMemory: cache
				    toSize: cacheLength + bufferLength];







>







787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
					memcpy(newCache, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength = bufferLength - i - 1;

				waitingForDelimiter = NO;
				return ret;
			}
		}

		/* Neither the delimiter nor \0 was found */
		cache = [self resizeMemory: cache
				    toSize: cacheLength + bufferLength];
811
812
813
814
815
816
817

818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
			    bufferLength);

		cacheLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}


	return nil;
}


- (OFString*)readTillDelimiter: (OFString*)delimiter
{
	return [self readTillDelimiter: delimiter
			  withEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (of_string_encoding_t)encoding
{
	OFString *ret = nil;

	if ((ret = [self _tryReadTillDelimiter: delimiter
				  withEncoding: encoding
				    checkCache: YES]) != nil)
		return ret;

	while ((ret = [self _tryReadTillDelimiter: delimiter
				     withEncoding: encoding
				       checkCache: NO]) == nil)
		if ([self isAtEndOfStream])
			return nil;

	return ret;
}

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
{
	return [self tryReadTillDelimiter: delimiter
			     withEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
		     withEncoding: (of_string_encoding_t)encoding
{
	return [self _tryReadTillDelimiter: delimiter
			      withEncoding: encoding
				checkCache: YES];
}

- (BOOL)buffersWrites
{
	return buffersWrites;
}

- (void)setBuffersWrites: (BOOL)enable
{







>















<
<
<
<

|
|
<












<
<
<
<
<
<
<
<







809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831




832
833
834

835
836
837
838
839
840
841
842
843
844
845
846








847
848
849
850
851
852
853
			    bufferLength);

		cacheLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	waitingForDelimiter = YES;
	return nil;
}


- (OFString*)readTillDelimiter: (OFString*)delimiter
{
	return [self readTillDelimiter: delimiter
			  withEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (of_string_encoding_t)encoding
{
	OFString *ret = nil;






	while ((ret = [self tryReadTillDelimiter: delimiter
				    withEncoding: encoding]) == nil)

		if ([self isAtEndOfStream])
			return nil;

	return ret;
}

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
{
	return [self tryReadTillDelimiter: delimiter
			     withEncoding: OF_STRING_ENCODING_UTF_8];
}









- (BOOL)buffersWrites
{
	return buffersWrites;
}

- (void)setBuffersWrites: (BOOL)enable
{
1400
1401
1402
1403
1404
1405
1406





1407
}

- (void)close
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}





@end







>
>
>
>
>

1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
}

- (void)close
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (BOOL)_isWaitingForDelimiter
{
	return waitingForDelimiter;
}
@end

Modified src/OFStreamObserver.h from [753eef6385] to [aece4ad798].

38
39
40
41
42
43
44







45
46
47
48
49
50
51
@protocol OFStreamObserverDelegate
#endif
#ifdef OF_HAVE_OPTIONAL_PROTOCOLS
@optional
#endif
/**
 * \brief This callback is called when a stream did get ready for reading.







 *
 * \param stream The stream which did become ready for reading
 */
- (void)streamIsReadyForReading: (OFStream*)stream;

/**
 * \brief This callback is called when a stream did get ready for writing.







>
>
>
>
>
>
>







38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@protocol OFStreamObserverDelegate
#endif
#ifdef OF_HAVE_OPTIONAL_PROTOCOLS
@optional
#endif
/**
 * \brief This callback is called when a stream did get ready for reading.
 *
 * NOTE: When -[tryReadLine] or -[tryReadTillDelimiter:] has been called on the
 *	 the stream, this callback will not be called again until new data has
 *	 been received, even though there is still data in the cache. The reason
 *	 for this is to prevent spinning in a loop when there is an incomplete
 *	 string in the cache. Once the string is complete, the callback will be
 *	 called again if there is data in the cache.
 *
 * \param stream The stream which did become ready for reading
 */
- (void)streamIsReadyForReading: (OFStream*)stream;

/**
 * \brief This callback is called when a stream did get ready for writing.

Modified src/OFStreamObserver.m from [26a8940bc6] to [332048c7be].

352
353
354
355
356
357
358
359

360
361
362
363
364
365
366
	OFStream **cArray = [readStreams cArray];
	size_t i, count = [readStreams count];
	BOOL foundInCache = NO;

	pool = [[OFAutoreleasePool alloc] init];

	for (i = 0; i < count; i++) {
		if ([cArray[i] pendingBytes] > 0) {

			[delegate streamIsReadyForReading: cArray[i]];
			foundInCache = YES;
			[pool releaseObjects];
		}
	}

	[pool release];







|
>







352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
	OFStream **cArray = [readStreams cArray];
	size_t i, count = [readStreams count];
	BOOL foundInCache = NO;

	pool = [[OFAutoreleasePool alloc] init];

	for (i = 0; i < count; i++) {
		if ([cArray[i] pendingBytes] > 0 &&
		    ![cArray[i] _isWaitingForDelimiter]) {
			[delegate streamIsReadyForReading: cArray[i]];
			foundInCache = YES;
			[pool releaseObjects];
		}
	}

	[pool release];