Remove unneeded memset calls

Also use constant for array lengths.
This commit is contained in:
Andrew Gaul 2020-09-14 19:49:45 +09:00
parent c7132b7f56
commit 5121c73ed1
3 changed files with 24 additions and 32 deletions

View File

@ -191,15 +191,15 @@ size_t get_md5_digest_length()
unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size) unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size)
{ {
struct md5_ctx ctx_md5; struct md5_ctx ctx_md5;
unsigned char buf[512];
off_t bytes; off_t bytes;
unsigned char* result; unsigned char* result;
memset(buf, 0, 512);
md5_init(&ctx_md5); md5_init(&ctx_md5);
for(off_t total = 0; total < size; total += bytes){ for(off_t total = 0; total < size; total += bytes){
bytes = 512 < (size - total) ? 512 : (size - total); off_t len = 512;
unsigned char buf[len];
bytes = len < (size - total) ? len : (size - total);
bytes = pread(fd, buf, bytes, start + total); bytes = pread(fd, buf, bytes, start + total);
if(0 == bytes){ if(0 == bytes){
// end of file // end of file
@ -210,7 +210,6 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size)
return NULL; return NULL;
} }
md5_update(&ctx_md5, bytes, buf); md5_update(&ctx_md5, bytes, buf);
memset(buf, 0, 512);
} }
result = new unsigned char[get_md5_digest_length()]; result = new unsigned char[get_md5_digest_length()];
md5_digest(&ctx_md5, get_md5_digest_length(), result); md5_digest(&ctx_md5, get_md5_digest_length(), result);
@ -224,7 +223,6 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size)
{ {
gcry_md_hd_t ctx_md5; gcry_md_hd_t ctx_md5;
gcry_error_t err; gcry_error_t err;
char buf[512];
off_t bytes; off_t bytes;
unsigned char* result; unsigned char* result;
@ -236,14 +234,15 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size)
size = st.st_size; size = st.st_size;
} }
memset(buf, 0, 512);
if(GPG_ERR_NO_ERROR != (err = gcry_md_open(&ctx_md5, GCRY_MD_MD5, 0))){ if(GPG_ERR_NO_ERROR != (err = gcry_md_open(&ctx_md5, GCRY_MD_MD5, 0))){
S3FS_PRN_ERR("MD5 context creation failure: %s/%s", gcry_strsource(err), gcry_strerror(err)); S3FS_PRN_ERR("MD5 context creation failure: %s/%s", gcry_strsource(err), gcry_strerror(err));
return NULL; return NULL;
} }
for(off_t total = 0; total < size; total += bytes){ for(off_t total = 0; total < size; total += bytes){
bytes = 512 < (size - total) ? 512 : (size - total); off_t len = 512;
char buf[len];
bytes = len < (size - total) ? len : (size - total);
bytes = pread(fd, buf, bytes, start + total); bytes = pread(fd, buf, bytes, start + total);
if(0 == bytes){ if(0 == bytes){
// end of file // end of file
@ -255,7 +254,6 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size)
return NULL; return NULL;
} }
gcry_md_write(ctx_md5, buf, bytes); gcry_md_write(ctx_md5, buf, bytes);
memset(buf, 0, 512);
} }
result = new unsigned char[get_md5_digest_length()]; result = new unsigned char[get_md5_digest_length()];
memcpy(result, gcry_md_read(ctx_md5, 0), get_md5_digest_length()); memcpy(result, gcry_md_read(ctx_md5, 0), get_md5_digest_length());
@ -291,15 +289,15 @@ bool s3fs_sha256(const unsigned char* data, unsigned int datalen, unsigned char*
unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size) unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size)
{ {
struct sha256_ctx ctx_sha256; struct sha256_ctx ctx_sha256;
unsigned char buf[512];
off_t bytes; off_t bytes;
unsigned char* result; unsigned char* result;
memset(buf, 0, 512);
sha256_init(&ctx_sha256); sha256_init(&ctx_sha256);
for(off_t total = 0; total < size; total += bytes){ for(off_t total = 0; total < size; total += bytes){
bytes = 512 < (size - total) ? 512 : (size - total); off_t len = 512;
unsigned char buf[len];
bytes = len < (size - total) ? len : (size - total);
bytes = pread(fd, buf, bytes, start + total); bytes = pread(fd, buf, bytes, start + total);
if(0 == bytes){ if(0 == bytes){
// end of file // end of file
@ -310,7 +308,6 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size)
return NULL; return NULL;
} }
sha256_update(&ctx_sha256, bytes, buf); sha256_update(&ctx_sha256, bytes, buf);
memset(buf, 0, 512);
} }
result = new unsigned char[get_sha256_digest_length()]; result = new unsigned char[get_sha256_digest_length()];
sha256_digest(&ctx_sha256, get_sha256_digest_length(), result); sha256_digest(&ctx_sha256, get_sha256_digest_length(), result);
@ -343,7 +340,6 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size)
{ {
gcry_md_hd_t ctx_sha256; gcry_md_hd_t ctx_sha256;
gcry_error_t err; gcry_error_t err;
char buf[512];
off_t bytes; off_t bytes;
unsigned char* result; unsigned char* result;
@ -355,14 +351,15 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size)
size = st.st_size; size = st.st_size;
} }
memset(buf, 0, 512);
if(GPG_ERR_NO_ERROR != (err = gcry_md_open(&ctx_sha256, GCRY_MD_SHA256, 0))){ if(GPG_ERR_NO_ERROR != (err = gcry_md_open(&ctx_sha256, GCRY_MD_SHA256, 0))){
S3FS_PRN_ERR("SHA256 context creation failure: %s/%s", gcry_strsource(err), gcry_strerror(err)); S3FS_PRN_ERR("SHA256 context creation failure: %s/%s", gcry_strsource(err), gcry_strerror(err));
return NULL; return NULL;
} }
for(off_t total = 0; total < size; total += bytes){ for(off_t total = 0; total < size; total += bytes){
bytes = 512 < (size - total) ? 512 : (size - total); off_t len = 512;
char buf[len];
bytes = len < (size - total) ? len : (size - total);
bytes = pread(fd, buf, bytes, start + total); bytes = pread(fd, buf, bytes, start + total);
if(0 == bytes){ if(0 == bytes){
// end of file // end of file
@ -374,7 +371,6 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size)
return NULL; return NULL;
} }
gcry_md_write(ctx_sha256, buf, bytes); gcry_md_write(ctx_sha256, buf, bytes);
memset(buf, 0, 512);
} }
result = new unsigned char[get_sha256_digest_length()]; result = new unsigned char[get_sha256_digest_length()];
memcpy(result, gcry_md_read(ctx_sha256, 0), get_sha256_digest_length()); memcpy(result, gcry_md_read(ctx_sha256, 0), get_sha256_digest_length());

View File

@ -153,7 +153,6 @@ size_t get_md5_digest_length()
unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size) unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size)
{ {
PK11Context* md5ctx; PK11Context* md5ctx;
unsigned char buf[512];
off_t bytes; off_t bytes;
unsigned char* result; unsigned char* result;
unsigned int md5outlen; unsigned int md5outlen;
@ -166,11 +165,12 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size)
size = st.st_size; size = st.st_size;
} }
memset(buf, 0, 512);
md5ctx = PK11_CreateDigestContext(SEC_OID_MD5); md5ctx = PK11_CreateDigestContext(SEC_OID_MD5);
for(off_t total = 0; total < size; total += bytes){ for(off_t total = 0; total < size; total += bytes){
bytes = 512 < (size - total) ? 512 : (size - total); off_t len = 512;
unsigned char buf[len];
bytes = len < (size - total) ? len : (size - total);
bytes = pread(fd, buf, bytes, start + total); bytes = pread(fd, buf, bytes, start + total);
if(0 == bytes){ if(0 == bytes){
// end of file // end of file
@ -182,7 +182,6 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size)
return NULL; return NULL;
} }
PK11_DigestOp(md5ctx, buf, bytes); PK11_DigestOp(md5ctx, buf, bytes);
memset(buf, 0, 512);
} }
result = new unsigned char[get_md5_digest_length()]; result = new unsigned char[get_md5_digest_length()];
PK11_DigestFinal(md5ctx, result, &md5outlen, get_md5_digest_length()); PK11_DigestFinal(md5ctx, result, &md5outlen, get_md5_digest_length());
@ -219,7 +218,6 @@ bool s3fs_sha256(const unsigned char* data, unsigned int datalen, unsigned char*
unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size) unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size)
{ {
PK11Context* sha256ctx; PK11Context* sha256ctx;
unsigned char buf[512];
off_t bytes; off_t bytes;
unsigned char* result; unsigned char* result;
unsigned int sha256outlen; unsigned int sha256outlen;
@ -232,11 +230,12 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size)
size = st.st_size; size = st.st_size;
} }
memset(buf, 0, 512);
sha256ctx = PK11_CreateDigestContext(SEC_OID_SHA256); sha256ctx = PK11_CreateDigestContext(SEC_OID_SHA256);
for(off_t total = 0; total < size; total += bytes){ for(off_t total = 0; total < size; total += bytes){
bytes = 512 < (size - total) ? 512 : (size - total); off_t len = 512;
unsigned char buf[len];
bytes = len < (size - total) ? len : (size - total);
bytes = pread(fd, buf, bytes, start + total); bytes = pread(fd, buf, bytes, start + total);
if(0 == bytes){ if(0 == bytes){
// end of file // end of file
@ -248,7 +247,6 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size)
return NULL; return NULL;
} }
PK11_DigestOp(sha256ctx, buf, bytes); PK11_DigestOp(sha256ctx, buf, bytes);
memset(buf, 0, 512);
} }
result = new unsigned char[get_sha256_digest_length()]; result = new unsigned char[get_sha256_digest_length()];
PK11_DigestFinal(sha256ctx, result, &sha256outlen, get_sha256_digest_length()); PK11_DigestFinal(sha256ctx, result, &sha256outlen, get_sha256_digest_length());

View File

@ -256,7 +256,6 @@ size_t get_md5_digest_length()
unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size) unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size)
{ {
MD5_CTX md5ctx; MD5_CTX md5ctx;
char buf[512];
off_t bytes; off_t bytes;
unsigned char* result; unsigned char* result;
@ -268,11 +267,12 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size)
size = st.st_size; size = st.st_size;
} }
memset(buf, 0, 512);
MD5_Init(&md5ctx); MD5_Init(&md5ctx);
for(off_t total = 0; total < size; total += bytes){ for(off_t total = 0; total < size; total += bytes){
bytes = 512 < (size - total) ? 512 : (size - total); const off_t len = 512;
char buf[len];
bytes = len < (size - total) ? len : (size - total);
bytes = pread(fd, buf, bytes, start + total); bytes = pread(fd, buf, bytes, start + total);
if(0 == bytes){ if(0 == bytes){
// end of file // end of file
@ -283,7 +283,6 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, off_t size)
return NULL; return NULL;
} }
MD5_Update(&md5ctx, buf, bytes); MD5_Update(&md5ctx, buf, bytes);
memset(buf, 0, 512);
} }
result = new unsigned char[get_md5_digest_length()]; result = new unsigned char[get_md5_digest_length()];
@ -319,7 +318,6 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size)
{ {
const EVP_MD* md = EVP_get_digestbyname("sha256"); const EVP_MD* md = EVP_get_digestbyname("sha256");
EVP_MD_CTX* sha256ctx; EVP_MD_CTX* sha256ctx;
char buf[512];
off_t bytes; off_t bytes;
unsigned char* result; unsigned char* result;
@ -334,9 +332,10 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size)
sha256ctx = EVP_MD_CTX_create(); sha256ctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(sha256ctx, md, NULL); EVP_DigestInit_ex(sha256ctx, md, NULL);
memset(buf, 0, 512);
for(off_t total = 0; total < size; total += bytes){ for(off_t total = 0; total < size; total += bytes){
bytes = 512 < (size - total) ? 512 : (size - total); const off_t len = 512;
char buf[len];
bytes = len < (size - total) ? len : (size - total);
bytes = pread(fd, buf, bytes, start + total); bytes = pread(fd, buf, bytes, start + total);
if(0 == bytes){ if(0 == bytes){
// end of file // end of file
@ -348,7 +347,6 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, off_t size)
return NULL; return NULL;
} }
EVP_DigestUpdate(sha256ctx, buf, bytes); EVP_DigestUpdate(sha256ctx, buf, bytes);
memset(buf, 0, 512);
} }
result = new unsigned char[get_sha256_digest_length()]; result = new unsigned char[get_sha256_digest_length()];
EVP_DigestFinal_ex(sha256ctx, result, NULL); EVP_DigestFinal_ex(sha256ctx, result, NULL);