Skip to content
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

lib/gis: fix potential integer overflow issues #5217

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions lib/gis/compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ int G_read_compressed(int fd, int rbytes, unsigned char *dst, int nbytes,

int G_write_compressed(int fd, unsigned char *src, int nbytes, int number)
{
int dst_sz, nwritten, err;
unsigned char *dst, compressed;

/* Catch errors */
Expand All @@ -335,13 +334,14 @@ int G_write_compressed(int fd, unsigned char *src, int nbytes, int number)
}

/* get upper bound of compressed size */
dst_sz = G_compress_bound(nbytes, number);
int dst_sz = G_compress_bound(nbytes, number);
if (NULL ==
(dst = (unsigned char *)G_calloc(dst_sz, sizeof(unsigned char))))
return -1;

/* Now just call G_compress() */
err = G_compress(src, nbytes, dst, dst_sz, number);
ssize_t err = G_compress(src, nbytes, dst, dst_sz, number);
size_t nwritten = 0;

/* If compression succeeded write compressed row,
* otherwise write uncompressed row. Compression will fail
Expand All @@ -356,12 +356,11 @@ int G_write_compressed(int fd, unsigned char *src, int nbytes, int number)
G_warning(_("Unable to write compression flag"));
return -1;
}
nwritten = 0;
do {
err = write(fd, dst + nwritten, dst_sz - nwritten);
if (err >= 0)
nwritten += err;
} while (err > 0 && nwritten < dst_sz);
} while (err > 0 && nwritten < (size_t)dst_sz);
if (err <= 0) {
if (err == 0)
G_warning(_("Unable to write %d bytes: nothing written"),
Expand All @@ -381,12 +380,11 @@ int G_write_compressed(int fd, unsigned char *src, int nbytes, int number)
G_warning(_("Unable to write compression flag"));
return -1;
}
nwritten = 0;
do {
err = write(fd, src + nwritten, nbytes - nwritten);
if (err >= 0)
nwritten += err;
} while (err > 0 && nwritten < nbytes);
} while (err > 0 && nwritten < (size_t)nbytes);
if (err <= 0) {
if (err == 0)
G_warning(_("Unable to write %d bytes: nothing written"),
Expand All @@ -406,32 +404,31 @@ int G_write_compressed(int fd, unsigned char *src, int nbytes, int number)
if (err < 0)
return -2;

return nwritten;
return (int)nwritten;
} /* G_write_compressed() */

int G_write_uncompressed(int fd, const unsigned char *src, int nbytes)
{
int err, nwritten;
unsigned char compressed;

/* Catch errors */
if (src == NULL || nbytes < 0)
return -1;

/* Write the compression flag */
compressed = G_COMPRESSED_NO;
unsigned char compressed = G_COMPRESSED_NO;
if (write(fd, &compressed, 1) != 1) {
G_warning(_("Unable to write compression flag"));
return -1;
}

ssize_t err = 0;
size_t nwritten = 0;

/* Now write the data */
nwritten = 0;
do {
err = write(fd, src + nwritten, nbytes - nwritten);
if (err > 0)
nwritten += err;
} while (err > 0 && nwritten < nbytes);
} while (err > 0 && nwritten < (size_t)nbytes);
if (err <= 0) {
if (err == 0)
G_warning(_("Unable to write %d bytes: nothing written"), nbytes);
Expand All @@ -440,14 +437,14 @@ int G_write_uncompressed(int fd, const unsigned char *src, int nbytes)
strerror(errno));
}

if (err < 0 || nwritten != nbytes)
if (err < 0 || nwritten != (size_t)nbytes)
return -1;

/* Account for extra compressed flag */
nwritten++;

/* That's all */
return nwritten;
return (int)nwritten;

} /* G_write_uncompressed() */

Expand Down
2 changes: 1 addition & 1 deletion lib/gis/copy_dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int G_recursive_copy(const char *src, const char *dst)
}

while ((len = read(fd, buf, sizeof(buf))) > 0) {
while (len && (len2 = write(fd2, buf, len)) >= 0)
while ((len > 0) && (len2 = write(fd2, buf, (size_t)len)) >= 0)
len -= len2;
}

Expand Down
Loading