Skip to content

Commit f6d000a

Browse files
committed
fix string termination with 0
1 parent 408a7a3 commit f6d000a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Sources/MetricsTestUtils/TestMetrics.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ extension TestMetrics.FullKey: Hashable {
143143

144144
extension TestMetrics {
145145
// ==== ------------------------------------------------------------------------------------------------------------
146+
146147
// MARK: Counter
147148

148149
/// Assert that the passed in `metric` is a ``TestCounter`` and return it for further executing assertions.
@@ -174,12 +175,14 @@ extension TestMetrics {
174175
}
175176

176177
// ==== ------------------------------------------------------------------------------------------------------------
178+
177179
// MARK: Gauge
178180

179181
/// Assert that the passed in `metric` is a ``TestRecorder`` and return it for further executing assertions.
180182
public func expectGauge(_ metric: Gauge) throws -> TestRecorder {
181183
return try self.expectRecorder(metric)
182184
}
185+
183186
/// Locate a ``TestRecorder`` created by the ``TestMetrics`` factory identified by the passed in ``label`` and ``dimensions``, and return it for further executing assertions.
184187
///
185188
/// - Parameters:
@@ -192,6 +195,7 @@ extension TestMetrics {
192195
}
193196

194197
// ==== ------------------------------------------------------------------------------------------------------------
198+
195199
// MARK: Recorder
196200

197201
/// Assert that the passed in `metric` is a ``TestRecorder`` and return it for further executing assertions.
@@ -223,6 +227,7 @@ extension TestMetrics {
223227
}
224228

225229
// ==== ------------------------------------------------------------------------------------------------------------
230+
226231
// MARK: Timer
227232

228233
/// Assert that the passed in `metric` is a ``TestTimer`` and return it for further executing assertions.

Sources/SystemMetrics/SystemMetrics.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ public enum SystemMetrics {
188188

189189
#if os(Linux)
190190
internal static func linuxSystemMetrics() -> SystemMetrics.Data? {
191-
class CFile {
191+
/// Minimal file reading implementation so we don't have to depend on Foundation.
192+
/// Designed only for the narrow use case of this library, reading `/proc/self/stat`.
193+
final class CFile {
192194
let path: String
193195

194196
private var file: UnsafeMutablePointer<FILE>?
@@ -221,7 +223,7 @@ public enum SystemMetrics {
221223
return nil
222224
}
223225
#if compiler(>=5.1)
224-
let buff: [CChar] = Array(unsafeUninitializedCapacity: 1024) { ptr, size in
226+
var buff: [CChar] = Array(unsafeUninitializedCapacity: 1024) { ptr, size in
225227
guard fgets(ptr.baseAddress, Int32(ptr.count), f) != nil else {
226228
if feof(f) != 0 {
227229
size = 0
@@ -230,9 +232,10 @@ public enum SystemMetrics {
230232
preconditionFailure("Error reading line")
231233
}
232234
}
233-
size = strlen(ptr.baseAddress!)
235+
size = strlen(ptr.baseAddress!) + 1 // the string + NULL to terminate it
234236
}
235237
if buff.isEmpty { return nil }
238+
buff[buff.index(before: buff.endIndex)] = 0 // ensure the string is null-terminated
236239
return String(cString: buff)
237240
#else
238241
var buff = [CChar](repeating: 0, count: 1024)

0 commit comments

Comments
 (0)