Browse code

Corrected int16 overflow and buffer sizes

Signed-off-by: Brendan Dixon <brendand@microsoft.com>
(cherry picked from commit a264e1e83debfaefcb99f5554b8eda9c088a09d1)

Brendan Dixon authored on 2015/04/09 08:35:37
Showing 1 changed files
... ...
@@ -410,25 +410,25 @@ func getNumberOfChars(fromCoord COORD, toCoord COORD, screenSize COORD) uint32 {
410 410
 
411 411
 var buffer []CHAR_INFO
412 412
 
413
-func clearDisplayRect(handle uintptr, fillChar rune, attributes WORD, fromCoord COORD, toCoord COORD, windowSize COORD) (uint32, error) {
413
+func clearDisplayRect(handle uintptr, attributes WORD, fromCoord COORD, toCoord COORD) (uint32, error) {
414 414
 	var writeRegion SMALL_RECT
415
-	writeRegion.Top = fromCoord.Y
416 415
 	writeRegion.Left = fromCoord.X
416
+	writeRegion.Top = fromCoord.Y
417 417
 	writeRegion.Right = toCoord.X
418 418
 	writeRegion.Bottom = toCoord.Y
419 419
 
420 420
 	// allocate and initialize buffer
421 421
 	width := toCoord.X - fromCoord.X + 1
422 422
 	height := toCoord.Y - fromCoord.Y + 1
423
-	size := width * height
423
+	size := uint32(width) * uint32(height)
424 424
 	if size > 0 {
425
-		for i := 0; i < int(size); i++ {
426
-			buffer[i].UnicodeChar = WCHAR(fillChar)
427
-			buffer[i].Attributes = attributes
425
+		buffer := make([]CHAR_INFO, size)
426
+		for i := range buffer {
427
+			buffer[i] = CHAR_INFO{WCHAR(' '), attributes}
428 428
 		}
429 429
 
430 430
 		// Write to buffer
431
-		r, err := writeConsoleOutput(handle, buffer[:size], windowSize, COORD{X: 0, Y: 0}, &writeRegion)
431
+		r, err := writeConsoleOutput(handle, buffer, COORD{X: width, Y: height}, COORD{X: 0, Y: 0}, &writeRegion)
432 432
 		if !r {
433 433
 			if err != nil {
434 434
 				return 0, err
... ...
@@ -439,18 +439,18 @@ func clearDisplayRect(handle uintptr, fillChar rune, attributes WORD, fromCoord
439 439
 	return uint32(size), nil
440 440
 }
441 441
 
442
-func clearDisplayRange(handle uintptr, fillChar rune, attributes WORD, fromCoord COORD, toCoord COORD, windowSize COORD) (uint32, error) {
442
+func clearDisplayRange(handle uintptr, attributes WORD, fromCoord COORD, toCoord COORD) (uint32, error) {
443 443
 	nw := uint32(0)
444 444
 	// start and end on same line
445 445
 	if fromCoord.Y == toCoord.Y {
446
-		return clearDisplayRect(handle, fillChar, attributes, fromCoord, toCoord, windowSize)
446
+		return clearDisplayRect(handle, attributes, fromCoord, toCoord)
447 447
 	}
448 448
 	// TODO(azlinux): if full screen, optimize
449 449
 
450 450
 	// spans more than one line
451 451
 	if fromCoord.Y < toCoord.Y {
452 452
 		// from start position till end of line for first line
453
-		n, err := clearDisplayRect(handle, fillChar, attributes, fromCoord, COORD{X: windowSize.X - 1, Y: fromCoord.Y}, windowSize)
453
+		n, err := clearDisplayRect(handle, attributes, fromCoord, COORD{X: toCoord.X, Y: fromCoord.Y})
454 454
 		if err != nil {
455 455
 			return nw, err
456 456
 		}
... ...
@@ -458,14 +458,14 @@ func clearDisplayRange(handle uintptr, fillChar rune, attributes WORD, fromCoord
458 458
 		// lines between
459 459
 		linesBetween := toCoord.Y - fromCoord.Y - 1
460 460
 		if linesBetween > 0 {
461
-			n, err = clearDisplayRect(handle, fillChar, attributes, COORD{X: 0, Y: fromCoord.Y + 1}, COORD{X: windowSize.X - 1, Y: toCoord.Y - 1}, windowSize)
461
+			n, err = clearDisplayRect(handle, attributes, COORD{X: 0, Y: fromCoord.Y + 1}, COORD{X: toCoord.X, Y: toCoord.Y - 1})
462 462
 			if err != nil {
463 463
 				return nw, err
464 464
 			}
465 465
 			nw += n
466 466
 		}
467 467
 		// lines at end
468
-		n, err = clearDisplayRect(handle, fillChar, attributes, COORD{X: 0, Y: toCoord.Y}, toCoord, windowSize)
468
+		n, err = clearDisplayRect(handle, attributes, COORD{X: 0, Y: toCoord.Y}, toCoord)
469 469
 		if err != nil {
470 470
 			return nw, err
471 471
 		}
... ...
@@ -715,9 +715,9 @@ func (term *WindowsTerminal) HandleOutputCommand(handle uintptr, command []byte)
715 715
 		switch value {
716 716
 		case 0:
717 717
 			start = screenBufferInfo.CursorPosition
718
-			// end of the screen
719
-			end.X = screenBufferInfo.MaximumWindowSize.X - 1
720
-			end.Y = screenBufferInfo.MaximumWindowSize.Y - 1
718
+			// end of the buffer
719
+			end.X = screenBufferInfo.Size.X - 1
720
+			end.Y = screenBufferInfo.Size.Y - 1
721 721
 			// cursor
722 722
 			cursor = screenBufferInfo.CursorPosition
723 723
 		case 1:
... ...
@@ -733,20 +733,21 @@ func (term *WindowsTerminal) HandleOutputCommand(handle uintptr, command []byte)
733 733
 			// start of the screen
734 734
 			start.X = 0
735 735
 			start.Y = 0
736
-			// end of the screen
737
-			end.X = screenBufferInfo.MaximumWindowSize.X - 1
738
-			end.Y = screenBufferInfo.MaximumWindowSize.Y - 1
736
+			// end of the buffer
737
+			end.X = screenBufferInfo.Size.X - 1
738
+			end.Y = screenBufferInfo.Size.Y - 1
739 739
 			// cursor
740 740
 			cursor.X = 0
741 741
 			cursor.Y = 0
742 742
 		}
743
-		if _, err := clearDisplayRange(uintptr(handle), ' ', term.screenBufferInfo.Attributes, start, end, screenBufferInfo.MaximumWindowSize); err != nil {
743
+		if _, err := clearDisplayRange(uintptr(handle), term.screenBufferInfo.Attributes, start, end); err != nil {
744 744
 			return n, err
745 745
 		}
746 746
 		// remember the the cursor position is 1 based
747 747
 		if err := setConsoleCursorPosition(handle, false, int16(cursor.X), int16(cursor.Y)); err != nil {
748 748
 			return n, err
749 749
 		}
750
+
750 751
 	case "K":
751 752
 		// [K
752 753
 		// Clears all characters from the cursor position to the end of the line (including the character at the cursor position).
... ...
@@ -766,7 +767,7 @@ func (term *WindowsTerminal) HandleOutputCommand(handle uintptr, command []byte)
766 766
 			// start is where cursor is
767 767
 			start = screenBufferInfo.CursorPosition
768 768
 			// end of line
769
-			end.X = screenBufferInfo.MaximumWindowSize.X - 1
769
+			end.X = screenBufferInfo.Size.X - 1
770 770
 			end.Y = screenBufferInfo.CursorPosition.Y
771 771
 			// cursor remains the same
772 772
 			cursor = screenBufferInfo.CursorPosition
... ...
@@ -782,15 +783,15 @@ func (term *WindowsTerminal) HandleOutputCommand(handle uintptr, command []byte)
782 782
 		case 2:
783 783
 			// start of the line
784 784
 			start.X = 0
785
-			start.Y = screenBufferInfo.MaximumWindowSize.Y - 1
785
+			start.Y = screenBufferInfo.CursorPosition.Y - 1
786 786
 			// end of the line
787
-			end.X = screenBufferInfo.MaximumWindowSize.X - 1
788
-			end.Y = screenBufferInfo.MaximumWindowSize.Y - 1
787
+			end.X = screenBufferInfo.Size.X - 1
788
+			end.Y = screenBufferInfo.CursorPosition.Y - 1
789 789
 			// cursor
790 790
 			cursor.X = 0
791
-			cursor.Y = screenBufferInfo.MaximumWindowSize.Y - 1
791
+			cursor.Y = screenBufferInfo.CursorPosition.Y - 1
792 792
 		}
793
-		if _, err := clearDisplayRange(uintptr(handle), ' ', term.screenBufferInfo.Attributes, start, end, screenBufferInfo.MaximumWindowSize); err != nil {
793
+		if _, err := clearDisplayRange(uintptr(handle), term.screenBufferInfo.Attributes, start, end); err != nil {
794 794
 			return n, err
795 795
 		}
796 796
 		// remember the the cursor position is 1 based