Skip to content

Commit 1d6189f

Browse files
authored
Merge pull request #27 from lfborjas/fix-tests-memory-patch
Fix tests, memory patch
2 parents 7edfb51 + d930b17 commit 1d6189f

File tree

7 files changed

+36
-13
lines changed

7 files changed

+36
-13
lines changed

.github/workflows/haskell.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
branches: [ master ]
66
pull_request:
7-
branches: [ master ]
7+
branches: [ master, rc ]
88

99
jobs:
1010
build:

ChangeLog.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog for swiss-ephemeris
22

3+
## v1.3.0.1
4+
5+
A couple of memory safety patches:
6+
7+
* Attempt to rein in memory unsafety by keeping all pointer peeking in IO for gravGroup fns.
8+
* Always allocate 256 chars for error messages.
9+
* [dev] Bundle test ephemeris into the hackage tarball, to allow hackage CI and nixOS to
10+
successfully run tests.
11+
312
## v1.3.0.0 (2021-06-18)
413

514
* **Drops support for base < 4.10**, which effectively excludes GHC versions less

package.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: swiss-ephemeris
2-
version: 1.3.0.0
2+
version: 1.3.0.1
33
github: "lfborjas/swiss-ephemeris"
44
license: AGPL-3
55
author: "Luis Borjas Reyes"
@@ -8,6 +8,7 @@ maintainer: "swiss-ephemeris@lfborjas.com"
88
extra-source-files:
99
- README.md
1010
- ChangeLog.md
11+
- swedist/sweph_18/*.se1
1112

1213
# Metadata used when publishing your package
1314
synopsis: Haskell bindings for the Swiss Ephemeris C library

src/SwissEphemeris.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ calculateObliquity time = do
175175
-- ones!
176176
calculateCoordinates' :: CalcFlag -> JulianTime -> PlanetNumber -> IO (Either String [Double])
177177
calculateCoordinates' options time planet =
178-
allocaArray 6 $ \coords -> withCAString "" $ \serr -> do
178+
allocaArray 6 $ \coords -> allocaErrorMessage $ \serr -> do
179179
iflgret <-
180180
c_swe_calc_ut
181181
(realToFrac . unJulianTime $ time)
@@ -293,7 +293,7 @@ calculateHousePositionSimple sys time loc pos = do
293293
-- in those cases, see `calculateHousePositionSimple`.
294294
calculateHousePosition :: HouseSystem -> Double -> GeographicPosition -> ObliquityInformation -> EclipticPosition -> IO (Either String HousePosition)
295295
calculateHousePosition sys armc' geoCoords obliq eclipticCoords =
296-
withArray [realToFrac $ lng eclipticCoords, realToFrac $ lat eclipticCoords] $ \xpin -> withCAString "" $ \serr -> do
296+
withArray [realToFrac $ lng eclipticCoords, realToFrac $ lat eclipticCoords] $ \xpin -> allocaErrorMessage $ \serr -> do
297297
housePos <-
298298
c_swe_house_pos
299299
(realToFrac armc')

src/SwissEphemeris/ChartUtils.hs

+8-6
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ gravGroup sz positions sectors =
111111
unsafePerformIO $ do
112112
withArray (map (planetPositionToGlyph sz) positions) $ \grobs ->
113113
withArray (map realToFrac sectors) $ \sbdy ->
114-
withCAString "" $ \serr -> do
114+
allocaErrorMessage $ \serr -> do
115115
let nob = fromIntegral $ length positions
116116
nsectors = fromIntegral $ length sectors - 1
117117
retval <-
@@ -122,7 +122,8 @@ gravGroup sz positions sectors =
122122
pure $ Left msg
123123
else do
124124
repositioned <- peekArray (fromIntegral nob) grobs
125-
pure . Right $ map glyphInfo repositioned
125+
glyphInfos <- mapM glyphInfo repositioned
126+
pure . Right $ glyphInfos
126127

127128
-- | /Easy/ version of 'gravGroup' that assumes:
128129
--
@@ -166,7 +167,7 @@ gravGroup2 sz positions sectors allowShift =
166167
in unsafePerformIO $ do
167168
withArray (map (planetPositionToGlyph sz) positions) $ \grobs ->
168169
withArray (map realToFrac sectors') $ \sbdy ->
169-
withCAString "" $ \serr -> do
170+
allocaErrorMessage $ \serr -> do
170171
let nob = fromIntegral $ length positions
171172
-- empty sector lists are allowed:
172173
nsectors = max 0 $ fromIntegral $ length sectors - 1
@@ -179,7 +180,8 @@ gravGroup2 sz positions sectors allowShift =
179180
pure $ Left msg
180181
else do
181182
repositioned <- peekArray (fromIntegral nob) grobs
182-
pure . Right $ map glyphInfo repositioned
183+
glyphInfos <- mapM glyphInfo repositioned
184+
pure . Right $ glyphInfos
183185

184186

185187
-- | /Easy/ version of 'gravGroup2', same provisions as 'gravGroupEasy'
@@ -215,8 +217,8 @@ planetPositionToGlyph (lwidth, rwidth) (planet, pos) = unsafePerformIO $ do
215217
, dp = planetPtr
216218
}
217219

218-
glyphInfo :: PlanetGlyph -> PlanetGlyphInfo
219-
glyphInfo GravityObject{pos, lsize, rsize, ppos, sector_no, sequence_no, level_no, scale, dp} = unsafePerformIO $ do
220+
glyphInfo :: PlanetGlyph -> IO PlanetGlyphInfo
221+
glyphInfo GravityObject{pos, lsize, rsize, ppos, sector_no, sequence_no, level_no, scale, dp} = do
220222
planet' <- peek dp
221223
pure $
222224
GlyphInfo {

src/SwissEphemeris/Internal.hs

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module SwissEphemeris.Internal where
99

1010
import Data.Bits
1111
import Data.Char (ord)
12-
import Foreign (Int32, castPtr)
12+
import Foreign (Int32, castPtr, allocaArray, Ptr)
1313
import Foreign.C.Types
1414
import Foreign.SwissEphemeris
1515
import GHC.Generics
@@ -326,3 +326,11 @@ planetNumber p = PlanetNumber $ CInt y
326326
numberToPlanet :: PlanetNumber -> Planet
327327
numberToPlanet (PlanetNumber (CInt n)) =
328328
toEnum . fromIntegral $ n
329+
330+
-- | As per the programmers manual, error output strings
331+
-- should accommodate at most 256 characters:
332+
-- see @sweodef.h#266@ and the manual:
333+
-- https://www.astro.com/swisseph/swephprg.htm
334+
-- in e.g.
335+
allocaErrorMessage :: (Ptr CChar -> IO b) -> IO b
336+
allocaErrorMessage = allocaArray 256

swiss-ephemeris.cabal

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 6c21f106ee90782f9b105cc708ee7e2f928126c74260238af93fb1673b03e365
7+
-- hash: 72302ec5122b3419ff8eba21d0affbe5a8e9e9839ca792c61e2bcd38b2aff97e
88

99
name: swiss-ephemeris
10-
version: 1.3.0.0
10+
version: 1.3.0.1
1111
synopsis: Haskell bindings for the Swiss Ephemeris C library
1212
description: Please see the README on GitHub at <https://github.com/lfborjas/swiss-ephemeris#readme>
1313
category: Data, Astrology
@@ -21,6 +21,9 @@ build-type: Simple
2121
extra-source-files:
2222
README.md
2323
ChangeLog.md
24+
swedist/sweph_18/seas_18.se1
25+
swedist/sweph_18/semo_18.se1
26+
swedist/sweph_18/sepl_18.se1
2427

2528
source-repository head
2629
type: git

0 commit comments

Comments
 (0)