Skip to content

Commit

Permalink
Handling undefined behavior in inffast_chunk
Browse files Browse the repository at this point in the history
This ports a21a4e8 - "Handling undefined behavior in inffast_chunk" from the Chromium fork of zlib.
  • Loading branch information
janaknat authored and vkrasnov committed Mar 11, 2021
1 parent 4a94dad commit 959b4ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions chunkcopy.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,26 @@ static inline unsigned char FAR* chunkcopy_lapped_safe(
return chunkcopy_lapped_relaxed(out, dist, len);
}

/* TODO(cavalcanti): see crbug.com/1110083. */
static inline unsigned char FAR* chunkcopy_safe_ugly(unsigned char FAR* out,
unsigned dist,
unsigned len,
unsigned char FAR* limit) {
#if defined(__GNUC__) && !defined(__clang__)
/* Speed is the same as using chunkcopy_safe
w/ GCC on ARM (tested gcc 6.3 and 7.5) and avoids
undefined behavior.
*/
return chunkcopy_core_safe(out, out - dist, len, limit);
#elif defined(__clang__) && !defined(__aarch64__)
/* Seems to perform better on 32bit (i.e. Android). */
return chunkcopy_core_safe(out, out - dist, len, limit);
#else
/* Seems to perform better on 64-bit. */
return chunkcopy_lapped_safe(out, dist, len, limit);
#endif
}

/*
* The chunk-copy code above deals with writing the decoded DEFLATE data to
* the output with SIMD methods to increase decode speed. Reading the input
Expand Down
2 changes: 1 addition & 1 deletion inffast_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */
the main copy is near the end.
*/
out = chunkunroll_relaxed(out, &dist, &len);
out = chunkcopy_safe(out, out - dist, len, limit);
out = chunkcopy_safe_ugly(out, dist, len, limit);
} else {
/* from points to window, so there is no risk of
overlapping pointers requiring memset-like behaviour
Expand Down

0 comments on commit 959b4ea

Please sign in to comment.