summaryrefslogtreecommitdiff
path: root/src/rust/protover/protover.rs
diff options
context:
space:
mode:
authorIsis Lovecruft <isis@torproject.org>2018-02-06 14:15:33 +0000
committerIsis Lovecruft <isis@torproject.org>2018-02-07 22:51:58 +0000
commitb85436c5961d1b2168c3954a7e718e03a121473f (patch)
tree6b03f9cc9384c31cb17bf4c92bf67c1259e6141d /src/rust/protover/protover.rs
parent7ea9e080c58f16fecefa5a0a20406635a2034366 (diff)
downloadtor-b85436c5961d1b2168c3954a7e718e03a121473f.tar.gz
tor-b85436c5961d1b2168c3954a7e718e03a121473f.zip
protover: Fix memleak in Rust impl of protover_compute_for_old_tor.
* FIXES #25127: https://bugs.torproject.org/25127 * ADDS a new module to the Rust tor_util crate for small utilities for working with static strings between languages. * CHANGES the return type of protover_compute_for_old_tor to point to immutable data. * CHANGES the code from the previous commit to use the new static string utilities.
Diffstat (limited to 'src/rust/protover/protover.rs')
-rw-r--r--src/rust/protover/protover.rs33
1 files changed, 14 insertions, 19 deletions
diff --git a/src/rust/protover/protover.rs b/src/rust/protover/protover.rs
index 00fe084083..1680d3394e 100644
--- a/src/rust/protover/protover.rs
+++ b/src/rust/protover/protover.rs
@@ -10,6 +10,7 @@ use std::collections::{HashMap, HashSet};
use std::ops::Range;
use std::string::String;
+use tor_util::strings::NUL_BYTE;
/// The first version of Tor that included "proto" entries in its descriptors.
/// Authorities should use this to decide whether to guess proto lines.
@@ -724,11 +725,11 @@ pub fn is_supported_here(proto: Proto, vers: u32) -> bool {
///
/// # Inputs
///
-/// * `version`, a string comprised of "[0-9,-]"
+/// * `version`, a string comprised of "[0-9a-z.-]"
///
/// # Returns
///
-/// A `String` whose value is series of pairs, comprising of the protocol name
+/// A `&'static [u8]` whose value is series of pairs, comprising of the protocol name
/// and versions that it supports. The string takes the following format:
///
/// "HSDir=1-1 LinkAuth=1"
@@ -737,33 +738,27 @@ pub fn is_supported_here(proto: Proto, vers: u32) -> bool {
/// only for tor versions older than FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS.
///
/// C_RUST_COUPLED: src/rust/protover.c `compute_for_old_tor`
-pub fn compute_for_old_tor(version: &str) -> String {
- if c_tor_version_as_new_as(
- version,
- FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS,
- )
- {
- return String::new();
+pub fn compute_for_old_tor(version: &str) -> &'static [u8] {
+ if c_tor_version_as_new_as(version, FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS) {
+ return NUL_BYTE;
}
if c_tor_version_as_new_as(version, "0.2.9.1-alpha") {
- let ret = "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 \
- Link=1-4 LinkAuth=1 Microdesc=1-2 Relay=1-2";
- return String::from(ret);
+ return b"Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 \
+ Link=1-4 LinkAuth=1 Microdesc=1-2 Relay=1-2\0";
}
if c_tor_version_as_new_as(version, "0.2.7.5") {
- let ret = "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 \
- Link=1-4 LinkAuth=1 Microdesc=1-2 Relay=1-2";
- return String::from(ret);
+ return b"Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 \
+ Link=1-4 LinkAuth=1 Microdesc=1-2 Relay=1-2\0";
}
if c_tor_version_as_new_as(version, "0.2.4.19") {
- let ret = "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 \
- Link=1-4 LinkAuth=1 Microdesc=1 Relay=1-2";
- return String::from(ret);
+ return b"Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 \
+ Link=1-4 LinkAuth=1 Microdesc=1 Relay=1-2\0";
}
- String::new()
+
+ NUL_BYTE
}
#[cfg(test)]