-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix C++ style casts #19
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
#include <iostream> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
#include <gzip/config.hpp> | ||
|
||
// zlib | ||
#include <zlib.h> | ||
// std | ||
#include <limits> | ||
#include <string> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, thank you for noticing - fixed in 912a7f8 |
||
#include <stdexcept> | ||
|
||
namespace gzip { | ||
|
@@ -31,7 +33,7 @@ std::string compress(const char* data, | |
{ | ||
throw std::runtime_error("deflate init failed"); | ||
} | ||
deflate_s.next_in = (Bytef*)data; | ||
deflate_s.next_in = reinterpret_cast<z_const Bytef*>(data); | ||
|
||
#ifdef DEBUG | ||
// Verify if size input will fit into unsigned int, type used for zlib's avail_in | ||
|
@@ -57,7 +59,7 @@ std::string compress(const char* data, | |
// There is no way we see that "increase" would not fit in an unsigned int, | ||
// hence we use static cast here to avoid -Wshorten-64-to-32 error | ||
deflate_s.avail_out = static_cast<unsigned int>(increase); | ||
deflate_s.next_out = (Bytef*)(output.data() + length); | ||
deflate_s.next_out = reinterpret_cast<Bytef*>((&output[0] + length)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @springmeyer mind explaining why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Proximately because a
Which means the previous So our options are either:
In this case I'm casting since that is what the previous code did. Also because the only way I can think of making the types the same is to use a If you want to know the difference between |
||
// From http://www.zlib.net/zlib_how.html | ||
// "deflate() has a return value that can indicate errors, yet we do not check it here. | ||
// Why not? Well, it turns out that deflate() can do no wrong here." | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea here is to define the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the idea to use this for other things as well? Like WERROR, for example? |
||
#ifndef ZLIB_CONST | ||
#define ZLIB_CONST | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
#include <gzip/config.hpp> | ||
|
||
// zlib | ||
#include <zlib.h> | ||
// std | ||
#include <limits> | ||
#include <string> | ||
#include <stdexcept> | ||
|
||
namespace gzip { | ||
|
@@ -25,7 +28,7 @@ std::string decompress(const char* data, std::size_t size) | |
{ | ||
throw std::runtime_error("inflate init failed"); | ||
} | ||
inflate_s.next_in = (Bytef*)data; | ||
inflate_s.next_in = reinterpret_cast<z_const Bytef*>(data); | ||
|
||
#ifdef DEBUG | ||
// Verify if size (long type) input will fit into unsigned int, type used for zlib's avail_in | ||
|
@@ -47,7 +50,7 @@ std::string decompress(const char* data, std::size_t size) | |
{ | ||
output.resize(length + 2 * size); | ||
inflate_s.avail_out = static_cast<unsigned int>(2 * size); | ||
inflate_s.next_out = (Bytef*)(output.data() + length); | ||
inflate_s.next_out = reinterpret_cast<Bytef*>(&output[0] + length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
int ret = inflate(&inflate_s, Z_FINISH); | ||
if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
#include <cstdlib> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This include is needed for this header to compile on its own, since |
||
namespace gzip { | ||
|
||
// These live in gzip.hpp because it doesnt need to use deps. | ||
// Otherwise, they would need to live in impl files if these methods used | ||
// zlib structures or functions like inflate/deflate) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^^ this is unrelated to fixing casts. Rather it is best practice to always include files the same way with
<>
consistently.