halephant-core 0.6.0

A proxy and pooler for Postgres
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Imperative byte cursor for decoding wire-protocol payloads.
//!
//! PostgreSQL messages are fixed-shape: the type byte determines exactly
//! which fields follow, there is no backtracking, and every malformed
//! payload is a hard error rather than an alternative to try. That
//! profile calls for a hand-written cursor, not a combinator library —
//! the same trade-off `sql/parser.rs` documents for the statement
//! walker. The cursor reads big-endian integers, C strings, and count-
//! or length-prefixed fields, and rejects hostile counts and lengths
//! against the remaining input before any allocation happens
//! (robustness review H1).

use crate::errors::ProtocolError;
use crate::proto::types;

// ---------------------------------------------------------------------------
// Entry point
// ---------------------------------------------------------------------------

/// Runs `decode` over `input`, requiring that every byte is consumed.
///
/// The caller hands the cursor exactly one message payload, so leftover
/// bytes after a fully decoded value mean the payload and the decoder
/// disagree about the message shape — a framing error, not padding.
///
/// The cursor's lifetime is tied to `input` (not higher-ranked) so that
/// method paths such as `Cursor::read_i32` are accepted as `decode`.
pub(in crate::proto) fn run<'a, O>(
    input: &'a [u8],
    decode: impl FnOnce(&mut Cursor<'a>) -> Result<O, ProtocolError>,
) -> Result<O, ProtocolError> {
    let mut cur = Cursor::new(input);
    let value = decode(&mut cur)?;
    cur.finish()?;
    Ok(value)
}

// ---------------------------------------------------------------------------
// Cursor
// ---------------------------------------------------------------------------

/// Linear reader over a single message payload.
///
/// Every read is bounds-checked: running past the end of the input
/// returns a [`ProtocolError`] instead of panicking, and counts or
/// lengths taken from the wire are validated against the remaining
/// input before any reservation. Decoding is a single forward pass with
/// no backtracking.
pub(in crate::proto) struct Cursor<'a> {
    input: &'a [u8],
    pos: usize,
}

impl<'a> Cursor<'a> {
    pub(in crate::proto) fn new(input: &'a [u8]) -> Self {
        Self { input, pos: 0 }
    }

    /// Byte offset of the next unread byte, for error positions.
    pub(in crate::proto) fn position(&self) -> usize {
        self.pos
    }

    /// Number of unread bytes.
    fn remaining(&self) -> usize {
        self.input.len() - self.pos
    }

    /// Builds the error for a read that runs past the end of the input.
    /// The reported position is the end of the input — the offset at
    /// which more data was expected.
    fn end_of_input(&self, needed: usize) -> ProtocolError {
        ProtocolError::InvalidValue {
            position: self.input.len(),
            message: format!(
                "unexpected end of input: need {needed} bytes, have {remaining}",
                remaining = self.remaining(),
            ),
        }
    }

    /// Consumes exactly `len` bytes and returns them as a slice of the
    /// original input.
    fn read_bytes(&mut self, len: usize) -> Result<&'a [u8], ProtocolError> {
        if len > self.remaining() {
            return Err(self.end_of_input(len));
        }
        let start = self.pos;
        self.pos += len;
        Ok(&self.input[start..self.pos])
    }

    /// Reads a single byte.
    pub(in crate::proto) fn read_u8(&mut self) -> Result<u8, ProtocolError> {
        let bytes = self.read_bytes(1)?;
        Ok(bytes[0])
    }

    /// Reads a big-endian `i16`.
    pub(in crate::proto) fn read_i16(&mut self) -> Result<i16, ProtocolError> {
        let bytes = self.read_bytes(2)?;
        Ok(i16::from_be_bytes([bytes[0], bytes[1]]))
    }

    /// Reads a big-endian `i32`.
    pub(in crate::proto) fn read_i32(&mut self) -> Result<i32, ProtocolError> {
        let bytes = self.read_bytes(4)?;
        Ok(i32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
    }

    /// Reads a null-terminated C string (UTF-8), consuming the
    /// terminator byte.
    pub(in crate::proto) fn read_cstring(&mut self) -> Result<String, ProtocolError> {
        let start = self.pos;
        let Some(len) = self.input[self.pos..].iter().position(|&b| b == 0) else {
            return Err(ProtocolError::InvalidValue {
                position: self.input.len(),
                message: "unterminated C string".to_owned(),
            });
        };
        // Validate before copying so invalid UTF-8 never allocates.
        let value = std::str::from_utf8(&self.input[start..start + len]).map_err(|e| {
            ProtocolError::InvalidValue {
                position: start,
                message: format!("invalid UTF-8: {e}"),
            }
        })?;
        self.pos = start + len + 1;
        Ok(value.to_owned())
    }

    /// Reads a length-prefixed nullable byte array (`i32` length).
    ///
    /// A negative length denotes NULL. The wire value for NULL is `-1`,
    /// but any negative length is accepted, matching the previous
    /// decoder.
    pub(in crate::proto) fn read_nullable_bytes(
        &mut self,
    ) -> Result<Option<Vec<u8>>, ProtocolError> {
        let start = self.pos;
        let len = self.read_i32()?;
        if len < 0 {
            return Ok(None);
        }
        // The length comes straight off the wire, so check it against
        // the remaining input before reserving: a tiny hostile frame
        // must not trigger a multi-gigabyte allocation.
        let len = len as usize;
        let remaining = self.remaining();
        if len > remaining {
            return Err(ProtocolError::InvalidValue {
                position: start,
                message: format!(
                    "declared length {len} exceeds remaining input ({remaining} bytes)"
                ),
            });
        }
        Ok(Some(self.read_bytes(len)?.to_vec()))
    }

    /// Reads an `i16` item count, then decodes that many items with
    /// `item`.
    pub(in crate::proto) fn read_count_prefixed<O>(
        &mut self,
        mut item: impl FnMut(&mut Self) -> Result<O, ProtocolError>,
    ) -> Result<Vec<O>, ProtocolError> {
        let start = self.pos;
        // The count is a signed wire value: reject negatives before the
        // cast so `-1` cannot become `usize::MAX` and panic
        // `Vec::with_capacity`.
        let raw = self.read_i16()?;
        if raw < 0 {
            return Err(ProtocolError::InvalidValue {
                position: start,
                message: format!("negative item count: {raw}"),
            });
        }
        // Every item consumes at least one input byte, so a count
        // beyond the remaining input is malformed; rejecting it here
        // bounds the reservation by the bytes actually received.
        let count = raw as usize;
        let remaining = self.remaining();
        if count > remaining {
            return Err(ProtocolError::InvalidValue {
                position: start,
                message: format!(
                    "declared count {count} exceeds remaining input ({remaining} bytes)"
                ),
            });
        }
        let mut items = Vec::with_capacity(count);
        for _ in 0..count {
            items.push(item(self)?);
        }
        Ok(items)
    }

    /// Reads an `i16` format code: `0` for text, `1` for binary.
    pub(in crate::proto) fn read_format_code(
        &mut self,
    ) -> Result<types::FormatCode, ProtocolError> {
        let start = self.pos;
        let raw = self.read_i16()?;
        match raw {
            0 => Ok(types::FormatCode::Text),
            1 => Ok(types::FormatCode::Binary),
            _ => Err(ProtocolError::InvalidValue {
                position: start,
                message: format!("invalid format code: {raw}"),
            }),
        }
    }

    /// Consumes and returns all remaining bytes, possibly none.
    pub(in crate::proto) fn take_rest(&mut self) -> &'a [u8] {
        let rest = &self.input[self.pos..];
        self.pos = self.input.len();
        rest
    }

    /// Requires that the input is fully consumed.
    fn finish(&self) -> Result<(), ProtocolError> {
        let remaining = self.remaining();
        if remaining > 0 {
            return Err(ProtocolError::InvalidValue {
                position: self.pos,
                message: format!("expected end of input ({remaining} trailing bytes)"),
            });
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    // ---- Integers --------------------------------------------------------

    #[test]
    fn read_u8_single_byte() {
        assert_eq!(run(&[7], Cursor::read_u8).unwrap(), 7);
    }

    #[test]
    fn read_u8_empty_input() {
        let err = run(&[], Cursor::read_u8).unwrap_err();
        assert!(matches!(err, ProtocolError::InvalidValue { .. }));
    }

    #[test]
    fn read_i16_big_endian() {
        assert_eq!(run(&7i16.to_be_bytes(), Cursor::read_i16).unwrap(), 7);
        assert_eq!(run(&(-2i16).to_be_bytes(), Cursor::read_i16).unwrap(), -2);
    }

    #[test]
    fn read_i16_truncated() {
        let err = run(&[0], Cursor::read_i16).unwrap_err();
        assert!(matches!(err, ProtocolError::InvalidValue { .. }));
    }

    #[test]
    fn read_i32_big_endian() {
        assert_eq!(run(&42i32.to_be_bytes(), Cursor::read_i32).unwrap(), 42);
        assert_eq!(run(&(-1i32).to_be_bytes(), Cursor::read_i32).unwrap(), -1);
    }

    #[test]
    fn read_i32_truncated() {
        let err = run(&[0, 0], Cursor::read_i32).unwrap_err();
        assert!(matches!(err, ProtocolError::InvalidValue { .. }));
    }

    // ---- C strings -------------------------------------------------------

    #[test]
    fn read_cstring_sequence() {
        let result = run(b"hello\0world\0", |cur| {
            Ok((cur.read_cstring()?, cur.read_cstring()?))
        })
        .unwrap();
        assert_eq!(result, ("hello".to_owned(), "world".to_owned()));
    }

    #[test]
    fn read_cstring_empty() {
        assert_eq!(run(b"\0", Cursor::read_cstring).unwrap(), String::new());
    }

    #[test]
    fn read_cstring_unterminated() {
        let err = run(b"hello", Cursor::read_cstring).unwrap_err();
        assert!(matches!(err, ProtocolError::InvalidValue { .. }));
    }

    #[test]
    fn read_cstring_invalid_utf8() {
        let err = run(&[0xFF, 0xFE, 0x00], Cursor::read_cstring).unwrap_err();
        assert!(
            matches!(err, ProtocolError::InvalidValue { .. })
                && err.to_string().contains("invalid UTF-8"),
            "expected an invalid-UTF-8 error, got: {err}"
        );
    }

    // ---- Count-prefixed fields -------------------------------------------

    #[test]
    fn read_count_prefixed_items() {
        let mut buf = Vec::new();
        buf.extend_from_slice(&3i16.to_be_bytes());
        buf.extend_from_slice(&10i32.to_be_bytes());
        buf.extend_from_slice(&20i32.to_be_bytes());
        buf.extend_from_slice(&30i32.to_be_bytes());

        let items = run(&buf, |cur| cur.read_count_prefixed(Cursor::read_i32)).unwrap();
        assert_eq!(items, vec![10, 20, 30]);
    }

    #[test]
    fn read_count_prefixed_negative_count() {
        // A count of 0xFFFF (-1 as i16) must fail before the cast can
        // become `usize::MAX` and panic `Vec::with_capacity`.
        let err = run(&[0xFF, 0xFF], |cur| {
            cur.read_count_prefixed(Cursor::read_i32)
        })
        .unwrap_err();
        assert!(
            matches!(err, ProtocolError::InvalidValue { .. })
                && err.to_string().contains("negative item count"),
            "expected a negative-count guard error, got: {err}"
        );
    }

    #[test]
    fn read_count_prefixed_count_exceeds_remaining() {
        let buf = i16::MAX.to_be_bytes();
        let err = run(&buf, |cur| cur.read_count_prefixed(Cursor::read_i32)).unwrap_err();
        assert!(
            matches!(err, ProtocolError::InvalidValue { .. })
                && err.to_string().contains("exceeds"),
            "expected a declared-count guard error, got: {err}"
        );
    }

    #[test]
    fn read_count_prefixed_guard_error_position_is_count_start() {
        // The guard error points at the count field, not at the end of
        // the input, so logs identify the hostile field directly.
        let err = run(&[0x00, 0xFF, 0xFF], |cur| {
            cur.read_u8()?;
            cur.read_count_prefixed(Cursor::read_i32)
        })
        .unwrap_err();
        assert!(
            matches!(err, ProtocolError::InvalidValue { position: 1, .. }),
            "expected the error at byte 1, got: {err}"
        );
    }

    // ---- Length-prefixed nullable bytes ----------------------------------

    #[test]
    fn read_nullable_bytes_present() {
        let mut buf = Vec::new();
        buf.extend_from_slice(&5i32.to_be_bytes());
        buf.extend_from_slice(b"hello");
        let value = run(&buf, Cursor::read_nullable_bytes).unwrap();
        assert_eq!(value, Some(b"hello".to_vec()));
    }

    #[test]
    fn read_nullable_bytes_null() {
        let value = run(&(-1i32).to_be_bytes(), Cursor::read_nullable_bytes).unwrap();
        assert_eq!(value, None);
    }

    #[test]
    fn read_nullable_bytes_any_negative_is_null() {
        // The previous decoder treated every negative length as NULL,
        // not just the documented `-1`; that behavior is preserved.
        let value = run(&(-5i32).to_be_bytes(), Cursor::read_nullable_bytes).unwrap();
        assert_eq!(value, None);
    }

    #[test]
    fn read_nullable_bytes_length_exceeds_remaining() {
        let mut buf = Vec::new();
        buf.extend_from_slice(&i32::MAX.to_be_bytes());
        buf.extend_from_slice(b"hi");
        let err = run(&buf, Cursor::read_nullable_bytes).unwrap_err();
        assert!(
            matches!(err, ProtocolError::InvalidValue { .. })
                && err.to_string().contains("exceeds"),
            "expected a declared-length guard error, got: {err}"
        );
    }

    #[test]
    fn read_nullable_bytes_truncated_value() {
        // Declares five bytes but delivers three: the declared length
        // fails the remaining-input check before any allocation.
        let mut buf = Vec::new();
        buf.extend_from_slice(&5i32.to_be_bytes());
        buf.extend_from_slice(b"abc");
        let err = run(&buf, Cursor::read_nullable_bytes).unwrap_err();
        assert!(
            matches!(err, ProtocolError::InvalidValue { .. })
                && err.to_string().contains("exceeds"),
            "expected a declared-length guard error, got: {err}"
        );
    }

    // ---- Format codes ----------------------------------------------------

    #[test]
    fn read_format_code_valid() {
        assert_eq!(
            run(&0i16.to_be_bytes(), Cursor::read_format_code).unwrap(),
            types::FormatCode::Text
        );
        assert_eq!(
            run(&1i16.to_be_bytes(), Cursor::read_format_code).unwrap(),
            types::FormatCode::Binary
        );
    }

    #[test]
    fn read_format_code_invalid() {
        let err = run(&99i16.to_be_bytes(), Cursor::read_format_code).unwrap_err();
        assert!(
            matches!(err, ProtocolError::InvalidValue { .. })
                && err.to_string().contains("invalid format code"),
            "expected a format-code error, got: {err}"
        );
    }

    // ---- Remaining input -------------------------------------------------

    #[test]
    fn take_rest_returns_remaining() {
        let rest = run(&[0, 1, b'x'], |cur| {
            cur.read_i16()?;
            Ok(cur.take_rest().to_vec())
        })
        .unwrap();
        assert_eq!(rest, b"x".to_vec());
    }

    #[test]
    fn take_rest_empty() {
        let rest = run(b"", |cur| Ok(cur.take_rest().to_vec())).unwrap();
        assert_eq!(rest, Vec::<u8>::new());
    }

    // ---- Exhaustion ------------------------------------------------------

    #[test]
    fn trailing_bytes_rejected() {
        let err = run(&[0, 0, 0, 1, 0xFF], Cursor::read_i32).unwrap_err();
        assert!(matches!(err, ProtocolError::InvalidValue { .. }));
    }

    #[test]
    fn exact_consumption_succeeds() {
        assert_eq!(run(&1i32.to_be_bytes(), Cursor::read_i32).unwrap(), 1);
    }
}