Skip to content

Commit e49a195

Browse files
author
John Hann
committed
Remove accidental usage of Seconds (Expiry) as unix time.
1 parent 22519fa commit e49a195

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

Network/Memcache/Protocol.hs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type Flags = Word32
4646

4747
data Expiry =
4848
Never |
49-
Seconds Word32 |
49+
Seconds Word32 | -- ^ Limited at run-time to 2592000 seconds (30 days).
5050
Date UTCTime
5151
deriving (Show)
5252
-- figure out how to limit seconds to the memcached limit of 30 days.
@@ -76,7 +76,7 @@ store :: (Key k, Serializable s) => String -> Connection -> Expiry -> Maybe Flag
7676
store action (Connection handle) expiry flags key val = do
7777
let valstr = serialize val
7878
let bytes = B.length valstr
79-
exptime <- expiryToWord expiry
79+
let exptime = expiryToWord expiry
8080
let cmd = unwords [action, toKey key, showFlags flags, show exptime, show bytes]
8181
hPutNetLn handle cmd
8282
hBSPutNetLn handle valstr
@@ -123,24 +123,18 @@ delete (Connection handle) key = do
123123
response <- hGetNetLn handle
124124
return (response == "DELETED")
125125

126-
expiryToWord :: Expiry -> IO Word32
126+
expiryToWord :: Expiry -> Word32
127127
expiryToWord expiry = do
128128
case expiry of
129-
Never -> return 0
130-
Date d -> return $ floor $ utcTimeToPOSIXSeconds d
129+
Never -> 0
130+
Date d -> floor (utcTimeToPOSIXSeconds d)
131131
Seconds s -> safeMemcachedSeconds s
132132

133133
thirtyDays = 30 * 24 * 60 * 60
134134

135-
safeMemcachedSeconds :: Word32 -> IO Word32
136-
safeMemcachedSeconds seconds = do
137-
if seconds <= thirtyDays
138-
-- fits within memcached "relative" range
139-
then return seconds
140-
-- "absolute" range. convert to a Unix time
141-
else (+ seconds) . floor <$> getPOSIXTime
135+
-- | Prevents accidental converstion to Unix time by memcached
136+
safeMemcachedSeconds :: Word32 -> Word32
137+
safeMemcachedSeconds seconds = min seconds thirtyDays
142138

143139
showFlags Nothing = "0"
144140
showFlags (Just f) = show f
145-
146-
-- vim: set ts=2 sw=2 et :

0 commit comments

Comments
 (0)