adding missing error check for fstat (#9532)

This commit is contained in:
Wen Hui 2021-09-29 00:10:33 -04:00 committed by GitHub
parent 4be2dd6ab9
commit 2c38caa176
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -142,9 +142,15 @@ int clusterLoadConfig(char *filename) {
}
}
if (redis_fstat(fileno(fp),&sb) == -1) {
serverLog(LL_WARNING,
"Unable to obtain the cluster node config file stat %s: %s",
filename, strerror(errno));
exit(1);
}
/* Check if the file is zero-length: if so return C_ERR to signal
* we have to write the config. */
if (fstat(fileno(fp),&sb) != -1 && sb.st_size == 0) {
if (sb.st_size == 0) {
fclose(fp);
return C_ERR;
}
@ -383,13 +389,14 @@ int clusterSaveConfig(int do_fsync) {
if ((fd = open(server.cluster_configfile,O_WRONLY|O_CREAT,0644))
== -1) goto err;
if (redis_fstat(fd,&sb) == -1) goto err;
/* Pad the new payload if the existing file length is greater. */
if (fstat(fd,&sb) != -1) {
if (sb.st_size > (off_t)content_size) {
ci = sdsgrowzero(ci,sb.st_size);
memset(ci+content_size,'\n',sb.st_size-content_size);
}
if (sb.st_size > (off_t)content_size) {
ci = sdsgrowzero(ci,sb.st_size);
memset(ci+content_size,'\n',sb.st_size-content_size);
}
if (write(fd,ci,sdslen(ci)) != (ssize_t)sdslen(ci)) goto err;
if (do_fsync) {
server.cluster->todo_before_sleep &= ~CLUSTER_TODO_FSYNC_CONFIG;