Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiplication overflow for macos #10

Merged
merged 1 commit into from Feb 26, 2019
Merged

Fix multiplication overflow for macos #10

merged 1 commit into from Feb 26, 2019

Conversation

simonrw
Copy link
Contributor

@simonrw simonrw commented Feb 25, 2019

The multiplication in percentage overflowed the u32 integer type before multiplication. Before, the operator precedence multiplied self.energy() by 100 which overflowed. The fix converts the energy and energy_full to floats and computes the ratio, before multiplying by 100 which prevents the overflow.

The multiplication in `percentage` overflowed the u32 integer type before multiplication. Before, the operator precedence multiplied `self.energy()` by 100 which overflowed. The fix converts the energy and energy_full to floats and computes the ratio, before multiplying by 100 which prevents the overflow.
@svartalf
Copy link
Owner

Could you also attach output from the ioreg -n AppleSmartBattery -r -a terminal command when the bug happens?

It would be nice to collect some real-life data finally and cover that case with test later.

@simonrw
Copy link
Contributor Author

simonrw commented Feb 25, 2019

Sure:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<dict>
		<key>AdapterInfo</key>
		<integer>0</integer>
		<key>Amperage</key>
		<integer>-13</integer>
		<key>AppleRawCurrentCapacity</key>
		<integer>6232</integer>
		<key>AppleRawMaxCapacity</key>
		<integer>6324</integer>
		<key>AvgTimeToEmpty</key>
		<integer>28763</integer>
		<key>AvgTimeToFull</key>
		<integer>65535</integer>
		<key>BatteryData</key>
		<dict>
			<key>CycleCount</key>
			<integer>943</integer>
			<key>DesignCapacity</key>
			<integer>8440</integer>
			<key>QmaxCell0</key>
			<integer>51740</integer>
			<key>QmaxCell1</key>
			<integer>3358</integer>
			<key>QmaxCell2</key>
			<integer>56604</integer>
			<key>ResScale</key>
			<integer>0</integer>
			<key>StateOfCharge</key>
			<integer>25344</integer>
			<key>Voltage</key>
			<integer>12701</integer>
		</dict>
		<key>BatteryFCCData</key>
		<dict>
			<key>DOD0</key>
			<integer>0</integer>
			<key>DOD1</key>
			<integer>0</integer>
			<key>DOD2</key>
			<integer>0</integer>
			<key>PassedCharge</key>
			<integer>0</integer>
			<key>ResScale</key>
			<integer>0</integer>
		</dict>
		<key>BatteryInstalled</key>
		<true/>
		<key>BatteryInvalidWakeSeconds</key>
		<integer>30</integer>
		<key>BatterySerialNumber</key>
		<string>D864323Y3K8F9CPA0</string>
		<key>BootPathUpdated</key>
		<integer>1547580984</integer>
		<key>CellVoltage</key>
		<array>
			<integer>4233</integer>
			<integer>4234</integer>
			<integer>4233</integer>
			<integer>0</integer>
		</array>
		<key>ChargerData</key>
		<dict>
			<key>ChargingCurrent</key>
			<integer>0</integer>
			<key>ChargingVoltage</key>
			<integer>20530</integer>
			<key>NotChargingReason</key>
			<integer>4</integer>
		</dict>
		<key>CurrentCapacity</key>
		<integer>6232</integer>
		<key>CycleCount</key>
		<integer>943</integer>
		<key>DesignCapacity</key>
		<integer>8440</integer>
		<key>DesignCycleCount70</key>
		<integer>65535</integer>
		<key>DesignCycleCount9C</key>
		<integer>1000</integer>
		<key>DeviceName</key>
		<string>bq20z451</string>
		<key>ExternalChargeCapable</key>
		<true/>
		<key>ExternalConnected</key>
		<true/>
		<key>FirmwareSerialNumber</key>
		<integer>1</integer>
		<key>FullPathUpdated</key>
		<integer>1551130983</integer>
		<key>FullyCharged</key>
		<true/>
		<key>IOGeneralInterest</key>
		<string>IOCommand is not serializable</string>
		<key>IOObjectClass</key>
		<string>AppleSmartBattery</string>
		<key>IOObjectRetainCount</key>
		<integer>6</integer>
		<key>IORegistryEntryID</key>
		<integer>4294967894</integer>
		<key>IORegistryEntryName</key>
		<string>AppleSmartBattery</string>
		<key>IOReportLegend</key>
		<array>
			<dict>
				<key>IOReportChannelInfo</key>
				<dict>
					<key>IOReportChannelUnit</key>
					<integer>0</integer>
				</dict>
				<key>IOReportChannels</key>
				<array>
					<array>
						<integer>7167869599145487988</integer>
						<integer>6460407809</integer>
						<string>BatteryCycleCount</string>
					</array>
				</array>
				<key>IOReportGroupName</key>
				<string>Battery</string>
			</dict>
		</array>
		<key>IOReportLegendPublic</key>
		<true/>
		<key>IOServiceBusyState</key>
		<integer>0</integer>
		<key>IOServiceBusyTime</key>
		<integer>35541</integer>
		<key>IOServiceState</key>
		<integer>30</integer>
		<key>InstantAmperage</key>
		<integer>0</integer>
		<key>InstantTimeToEmpty</key>
		<integer>65535</integer>
		<key>IsCharging</key>
		<false/>
		<key>LegacyBatteryInfo</key>
		<dict>
			<key>Amperage</key>
			<integer>-13</integer>
			<key>Capacity</key>
			<integer>6324</integer>
			<key>Current</key>
			<integer>6232</integer>
			<key>Cycle Count</key>
			<integer>943</integer>
			<key>Flags</key>
			<integer>5</integer>
			<key>Voltage</key>
			<integer>12701</integer>
		</dict>
		<key>Location</key>
		<integer>0</integer>
		<key>ManufactureDate</key>
		<integer>17670</integer>
		<key>Manufacturer</key>
		<string>SMP</string>
		<key>ManufacturerData</key>
		<data>
		AAAAAAcCAAESBgAAA0o0NQMwMDMDQVRMAxQA
		</data>
		<key>MaxCapacity</key>
		<integer>6324</integer>
		<key>MaxErr</key>
		<integer>1</integer>
		<key>OperationStatus</key>
		<integer>58435</integer>
		<key>PackReserve</key>
		<integer>200</integer>
		<key>PermanentFailureStatus</key>
		<integer>0</integer>
		<key>PostChargeWaitSeconds</key>
		<integer>120</integer>
		<key>PostDischargeWaitSeconds</key>
		<integer>120</integer>
		<key>Temperature</key>
		<integer>3062</integer>
		<key>TimeRemaining</key>
		<integer>26713</integer>
		<key>UserVisiblePathUpdated</key>
		<integer>1551131163</integer>
		<key>Voltage</key>
		<integer>12701</integer>
	</dict>
</array>
</plist>

@svartalf svartalf merged commit 6e8d962 into svartalf:master Feb 26, 2019
@svartalf
Copy link
Owner

@mindriot101, thank you for your contribution :)

svartalf added a commit that referenced this pull request Feb 26, 2019
@simonrw simonrw deleted the patch-1 branch February 26, 2019 17:48
davidkna pushed a commit to davidkna/rust-battery that referenced this pull request Jul 31, 2023
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants