Issue-#109 Bounds checking in Kernel.MakeFileName
Posted: Sat Mar 12, 2016 4:26 pm
				
				The first listing shows some test cases with the current code. The second listing shows the same tests with the proposed code below. 
For these tests name is declared as "ARRAY 10 OF CHAR".
With the old code line 9 is incorrect, and line 13 crashed.
			For these tests name is declared as "ARRAY 10 OF CHAR".
With the old code line 9 is incorrect, and line 13 crashed.
Code: Select all
       name          type   ->   name
   0   Fred          xYz         Fred.xyz
   1   Fred.         xYz         Fred
   2   Fred.abc      xYz         Fred.abc
   3   Fred                      Fred.odc
   4   Fred.                     Fred
   5   Fred.abc                  Fred.abc
   6   Alice.abc                 Alice.abc
   7   Alice         xYz         Alice.xyz
   8   Robert.ab                 Robert.ab
   9   Robert        xY          Robert
  10   Alice         pqr         Alice.pqr
  11   Robert        pqr         Robert
  12   Robert                    Robert
  13   Alice         pqrs        Code: Select all
       name          type   ->   name
   0   Fred          xYz         Fred.xyz
   1   Fred.         xYz         Fred
   2   Fred.abc      xYz         Fred.abc
   3   Fred                      Fred.odc
   4   Fred.                     Fred
   5   Fred.abc                  Fred.abc
   6   Alice.abc                 Alice.abc
   7   Alice         xYz         Alice.xyz
   8   Robert.ab                 Robert.ab
   9   Robert        xY          Robert.xy
  10   Alice         pqr         Alice.pqr
  11   Robert        pqr         Robert
  12   Robert                    Robert
  13   Alice         pqrs        Alice
Code: Select all
	PROCEDURE MakeFileName* (VAR name: ARRAY OF CHAR; type: ARRAY OF CHAR);
		VAR i, j, extLen: INTEGER; ext: ARRAY 8 OF CHAR; ch: CHAR;
	BEGIN
		i := 0;
		WHILE (name[i] # 0X) & (name[i] # ".") DO INC(i) END;
		IF name[i] = "." THEN
			IF name[i + 1] = 0X THEN name[i] := 0X END
		ELSE
			IF type = "" THEN extLen := 3 ELSE extLen := LEN(type$) END;
			IF i < LEN(name) - extLen - 1 THEN
				IF type = "" THEN ext := docType ELSE ext := type$ END;
				name[i] := "."; INC(i); j := 0; ch := ext[0];
				WHILE ch # 0X DO
					name[i] := Lower(ch); INC(i); INC(j); ch := ext[j]
				END;
				name[i] := 0X
			END
		END
	END MakeFileName;