Skip to content

Commit 35fff1b

Browse files
refi64sjoerdsimons
authored andcommittedMar 31, 2022
Avoid panics when failing to parse zip files
This does require the removal of the From implementation; it couldn't be converted to TryFrom due to lack of generic specialization (rust-lang/rust#50133). Signed-off-by: Ryan Gonzalez <ryan.gonzalez@collabora.com>
1 parent 177762a commit 35fff1b

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed
 

‎gitlab-runner/src/artifact.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
path::Path,
55
};
66

7-
use zip::read::ZipFile;
7+
use zip::{read::ZipFile, result::ZipResult};
88

99
/// A file in a gitlab artifact
1010
///
@@ -35,15 +35,6 @@ impl Read for ArtifactFile<'_> {
3535
pub(crate) trait ReadSeek: Read + Seek {}
3636
impl<T> ReadSeek for T where T: Read + Seek {}
3737

38-
impl<T> From<T> for Artifact
39-
where
40-
T: Send + Read + Seek + 'static,
41-
{
42-
fn from(data: T) -> Self {
43-
Artifact::new(Box::new(data))
44-
}
45-
}
46-
4738
/// An artifact downloaded from gitlab
4839
///
4940
/// The artifact holds a set of files which can be read out one by one
@@ -52,10 +43,9 @@ pub struct Artifact {
5243
}
5344

5445
impl Artifact {
55-
pub(crate) fn new(data: Box<dyn ReadSeek + Send>) -> Self {
56-
//let reader = std::io::Cursor::new(data);
57-
let zip = zip::ZipArchive::new(data).unwrap();
58-
Self { zip }
46+
pub(crate) fn new(data: Box<dyn ReadSeek + Send>) -> ZipResult<Self> {
47+
let zip = zip::ZipArchive::new(data)?;
48+
Ok(Self { zip })
5949
}
6050

6151
/// Iterator of the files names inside the artifacts

‎gitlab-runner/src/client.rs

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::time::Duration;
88
use thiserror::Error;
99
use tokio::io::{AsyncWrite, AsyncWriteExt};
1010
use url::Url;
11+
use zip::result::ZipError;
1112

1213
fn deserialize_null_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
1314
where
@@ -196,6 +197,8 @@ pub enum Error {
196197
Request(#[from] reqwest::Error),
197198
#[error("Failed to write to destination {0}")]
198199
WriteFailure(#[source] futures::io::Error),
200+
#[error("Failed to parse zip file: {0}")]
201+
ZipFile(#[from] ZipError),
199202
#[error("Empty trace")]
200203
EmptyTrace,
201204
}

‎gitlab-runner/src/job.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,15 @@ impl ArtifactCache {
200200
async fn get(&self, id: u64) -> Result<Option<Artifact>, ClientError> {
201201
if let Some(data) = self.lookup(id) {
202202
match data {
203-
CacheData::MemoryBacked(m) => Ok(Some(Artifact::from(std::io::Cursor::new(m)))),
203+
CacheData::MemoryBacked(m) => {
204+
Ok(Some(Artifact::new(Box::new(std::io::Cursor::new(m)))?))
205+
}
204206
CacheData::FileBacked(p) => {
205207
let f = tokio::fs::File::open(p)
206208
.await
207209
.map_err(ClientError::WriteFailure)?;
208210
// Always succeeds as no operations have been started
209-
Ok(Some(Artifact::from(f.try_into_std().unwrap())))
211+
Ok(Some(Artifact::new(Box::new(f.try_into_std().unwrap()))?))
210212
}
211213
}
212214
} else {

0 commit comments

Comments
 (0)
Please sign in to comment.