<table><tr><td style="">marten created this revision.<br />marten added a reviewer: KDE Games.<br />Herald added a subscriber: kde-games-devel.<br />marten requested review of this revision.
</td><a style="text-decoration: none; padding: 4px 8px; margin: 0 8px 8px; float: right; color: #464C5C; font-weight: bold; border-radius: 3px; background-color: #F7F7F9; background-image: linear-gradient(to bottom,#fff,#f1f0f1); display: inline-block; border: 1px solid rgba(71,87,120,.2);" href="https://phabricator.kde.org/D15716">View Revision</a></tr></table><br /><div><strong>REVISION SUMMARY</strong><div><p>The card deck is shuffled in shuffled() (in dealer.cpp) using a random number generator that, according to the comments above, is identical to the Freecell one.  I'm not familiar with the details of the Windows implementation, but the kpat implementation uses arithmetic operators on signed values which according to the C11 standard (quoted at <a href="https://wiki.sei.cmu.edu/confluence/display/c/INT13-C.+Use+bitwise+operators+only+on+unsigned+operands" class="remarkup-link" target="_blank" rel="noreferrer">https://wiki.sei.cmu.edu/confluence/display/c/INT13-C.+Use+bitwise+operators+only+on+unsigned+operands</a>) can give implementation defined results.</p>

<p>What I have observed when recompiling kpat recently (possibly after a switch to GCC 7.3.0) is the 'seed' value after a few iterations of the loop becoming negative due to integer overflow.  Then 'rand' also becomes negative (despite the '& 0x7fff'!), 'rand % i' again is negative and the result.swap() below segfaults.</p>

<p>Changing the type of the 'seed' variable to unsigned appears to avoid the problem, although I don't know whether this will be incompatible with the Freecell implementation.</p></div></div><br /><div><strong>TEST PLAN</strong><div><p>Run kpat without this change, select and deal a new game.  A segfault will regularly occur at the result.swap() line.<br />
Build and run kpat with this change, there is no segfault and the game is dealt correctly.</p></div></div><br /><div><strong>REPOSITORY</strong><div><div>R410 KPatience</div></div></div><br /><div><strong>REVISION DETAIL</strong><div><a href="https://phabricator.kde.org/D15716">https://phabricator.kde.org/D15716</a></div></div><br /><div><strong>AFFECTED FILES</strong><div><div>dealer.cpp</div></div></div><br /><div><strong>To: </strong>marten, KDE Games<br /><strong>Cc: </strong>kde-games-devel<br /></div>