Skip to content

Commit

Permalink
Merge pull request #27 from lfborjas/fix-tests-memory-patch
Browse files Browse the repository at this point in the history
Fix tests, memory patch
  • Loading branch information
lfborjas authored Jun 25, 2021
2 parents 7edfb51 + d930b17 commit 1d6189f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/haskell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
branches: [ master, rc ]

jobs:
build:
Expand Down
9 changes: 9 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog for swiss-ephemeris

## v1.3.0.1

A couple of memory safety patches:

* Attempt to rein in memory unsafety by keeping all pointer peeking in IO for gravGroup fns.
* Always allocate 256 chars for error messages.
* [dev] Bundle test ephemeris into the hackage tarball, to allow hackage CI and nixOS to
successfully run tests.

## v1.3.0.0 (2021-06-18)

* **Drops support for base < 4.10**, which effectively excludes GHC versions less
Expand Down
3 changes: 2 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: swiss-ephemeris
version: 1.3.0.0
version: 1.3.0.1
github: "lfborjas/swiss-ephemeris"
license: AGPL-3
author: "Luis Borjas Reyes"
Expand All @@ -8,6 +8,7 @@ maintainer: "[email protected]"
extra-source-files:
- README.md
- ChangeLog.md
- swedist/sweph_18/*.se1

# Metadata used when publishing your package
synopsis: Haskell bindings for the Swiss Ephemeris C library
Expand Down
4 changes: 2 additions & 2 deletions src/SwissEphemeris.hs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ calculateObliquity time = do
-- ones!
calculateCoordinates' :: CalcFlag -> JulianTime -> PlanetNumber -> IO (Either String [Double])
calculateCoordinates' options time planet =
allocaArray 6 $ \coords -> withCAString "" $ \serr -> do
allocaArray 6 $ \coords -> allocaErrorMessage $ \serr -> do
iflgret <-
c_swe_calc_ut
(realToFrac . unJulianTime $ time)
Expand Down Expand Up @@ -293,7 +293,7 @@ calculateHousePositionSimple sys time loc pos = do
-- in those cases, see `calculateHousePositionSimple`.
calculateHousePosition :: HouseSystem -> Double -> GeographicPosition -> ObliquityInformation -> EclipticPosition -> IO (Either String HousePosition)
calculateHousePosition sys armc' geoCoords obliq eclipticCoords =
withArray [realToFrac $ lng eclipticCoords, realToFrac $ lat eclipticCoords] $ \xpin -> withCAString "" $ \serr -> do
withArray [realToFrac $ lng eclipticCoords, realToFrac $ lat eclipticCoords] $ \xpin -> allocaErrorMessage $ \serr -> do
housePos <-
c_swe_house_pos
(realToFrac armc')
Expand Down
14 changes: 8 additions & 6 deletions src/SwissEphemeris/ChartUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ gravGroup sz positions sectors =
unsafePerformIO $ do
withArray (map (planetPositionToGlyph sz) positions) $ \grobs ->
withArray (map realToFrac sectors) $ \sbdy ->
withCAString "" $ \serr -> do
allocaErrorMessage $ \serr -> do
let nob = fromIntegral $ length positions
nsectors = fromIntegral $ length sectors - 1
retval <-
Expand All @@ -122,7 +122,8 @@ gravGroup sz positions sectors =
pure $ Left msg
else do
repositioned <- peekArray (fromIntegral nob) grobs
pure . Right $ map glyphInfo repositioned
glyphInfos <- mapM glyphInfo repositioned
pure . Right $ glyphInfos

-- | /Easy/ version of 'gravGroup' that assumes:
--
Expand Down Expand Up @@ -166,7 +167,7 @@ gravGroup2 sz positions sectors allowShift =
in unsafePerformIO $ do
withArray (map (planetPositionToGlyph sz) positions) $ \grobs ->
withArray (map realToFrac sectors') $ \sbdy ->
withCAString "" $ \serr -> do
allocaErrorMessage $ \serr -> do
let nob = fromIntegral $ length positions
-- empty sector lists are allowed:
nsectors = max 0 $ fromIntegral $ length sectors - 1
Expand All @@ -179,7 +180,8 @@ gravGroup2 sz positions sectors allowShift =
pure $ Left msg
else do
repositioned <- peekArray (fromIntegral nob) grobs
pure . Right $ map glyphInfo repositioned
glyphInfos <- mapM glyphInfo repositioned
pure . Right $ glyphInfos


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

glyphInfo :: PlanetGlyph -> PlanetGlyphInfo
glyphInfo GravityObject{pos, lsize, rsize, ppos, sector_no, sequence_no, level_no, scale, dp} = unsafePerformIO $ do
glyphInfo :: PlanetGlyph -> IO PlanetGlyphInfo
glyphInfo GravityObject{pos, lsize, rsize, ppos, sector_no, sequence_no, level_no, scale, dp} = do
planet' <- peek dp
pure $
GlyphInfo {
Expand Down
10 changes: 9 additions & 1 deletion src/SwissEphemeris/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module SwissEphemeris.Internal where

import Data.Bits
import Data.Char (ord)
import Foreign (Int32, castPtr)
import Foreign (Int32, castPtr, allocaArray, Ptr)
import Foreign.C.Types
import Foreign.SwissEphemeris
import GHC.Generics
Expand Down Expand Up @@ -326,3 +326,11 @@ planetNumber p = PlanetNumber $ CInt y
numberToPlanet :: PlanetNumber -> Planet
numberToPlanet (PlanetNumber (CInt n)) =
toEnum . fromIntegral $ n

-- | As per the programmers manual, error output strings
-- should accommodate at most 256 characters:
-- see @sweodef.h#266@ and the manual:
-- https://www.astro.com/swisseph/swephprg.htm
-- in e.g.
allocaErrorMessage :: (Ptr CChar -> IO b) -> IO b
allocaErrorMessage = allocaArray 256
7 changes: 5 additions & 2 deletions swiss-ephemeris.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 6c21f106ee90782f9b105cc708ee7e2f928126c74260238af93fb1673b03e365
-- hash: 72302ec5122b3419ff8eba21d0affbe5a8e9e9839ca792c61e2bcd38b2aff97e

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

source-repository head
type: git
Expand Down

0 comments on commit 1d6189f

Please sign in to comment.