[m-rev.] for review: timestamp.m: fix order-of-eval problem

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Nov 18 21:25:13 AEDT 2002


Estimated hours taken: 0.25
Branches: main, release

compiler/timestamp.m:
	Fix a bug where we were implicitly relying on order-of-evaluation:
	use an if-then-else, which the language reference manual says will
	guarantee sequential evaluation.

	(No regression test since this bug is not actually triggered
	by the current compiler.)

Workspace: /home/mars/fjh/ws1/mercury
Index: compiler/timestamp.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/timestamp.m,v
retrieving revision 1.5.2.1
diff -d -b -c -6 -r1.5.2.1 timestamp.m
*** compiler/timestamp.m	18 Nov 2002 10:14:26 -0000	1.5.2.1
--- compiler/timestamp.m	18 Nov 2002 10:18:25 -0000
***************
*** 121,160 ****
  		N = -1
  	).
  
  timestamp_to_string(timestamp(Timestamp)) = Timestamp.
  
  string_to_timestamp(Timestamp) = timestamp(Timestamp) :-
  	string__length(Timestamp) `with_type` int =
! 		string__length("yyyy-mm-dd hh:mm:ss"),
! 
  	string__to_int(string__unsafe_substring(Timestamp, 0, 4), _),
  
  	string__unsafe_index(Timestamp, 4, '-'),
  
! 	string__to_int(string__unsafe_substring(Timestamp, 5, 2), Month),
  	Month >= 1,
  	Month =< 12,
  
  	string__unsafe_index(Timestamp, 7, '-'),
  
  	string__to_int(string__unsafe_substring(Timestamp, 8, 2), Day),
  	Day >= 1,
  	Day =< 31,
  
  	string__unsafe_index(Timestamp, 10, ' '),
  
! 	string__to_int(string__unsafe_substring(Timestamp, 11, 2), Hour),
  	Hour >= 0,
  	Hour =< 23,
  
  	string__unsafe_index(Timestamp, 13, ':'),
  
! 	string__to_int(string__unsafe_substring(Timestamp, 14, 2), Minute),
  	Minute >= 0,
  	Minute =< 59,
  
  	string__unsafe_index(Timestamp, 16, ':'),
  
! 	string__to_int(string__unsafe_substring(Timestamp, 17, 2), Second),
  	Second >= 0,
! 	Second =< 61.	% Seconds 60 and 61 are for leap seconds.
--- 121,171 ----
  		N = -1
  	).
  
  timestamp_to_string(timestamp(Timestamp)) = Timestamp.
  
  string_to_timestamp(Timestamp) = timestamp(Timestamp) :-
+ 	% The if-then-else here is to force order of evaluation --
+ 	% we need to ensure that the length check occurs before the
+ 	% calls to unsafe_undex to avoid dereferencing invalid pointers.
+ 	(
  		string__length(Timestamp) `with_type` int =
! 			string__length("yyyy-mm-dd hh:mm:ss")
! 	->
  		string__to_int(string__unsafe_substring(Timestamp, 0, 4), _),
  
  		string__unsafe_index(Timestamp, 4, '-'),
  
! 		string__to_int(string__unsafe_substring(Timestamp, 5, 2),
! 			Month),
  		Month >= 1,
  		Month =< 12,
  
  		string__unsafe_index(Timestamp, 7, '-'),
  
  		string__to_int(string__unsafe_substring(Timestamp, 8, 2), Day),
  		Day >= 1,
  		Day =< 31,
  
  		string__unsafe_index(Timestamp, 10, ' '),
  
! 		string__to_int(string__unsafe_substring(Timestamp, 11, 2),
! 			Hour),
  		Hour >= 0,
  		Hour =< 23,
  
  		string__unsafe_index(Timestamp, 13, ':'),
  
! 		string__to_int(string__unsafe_substring(Timestamp, 14, 2),
! 			Minute),
  		Minute >= 0,
  		Minute =< 59,
  
  		string__unsafe_index(Timestamp, 16, ':'),
  
! 		string__to_int(string__unsafe_substring(Timestamp, 17, 2),
! 			Second),
  		Second >= 0,
! 		Second =< 61	% Seconds 60 and 61 are for leap seconds.
! 	;
! 		fail
! 	).
-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list