Skip to content

Commit

Permalink
protocol/client: Initialize list head to prevent NULL de-reference
Browse files Browse the repository at this point in the history
fixes: #2443
Change-Id: I86ef0270d41d6fb924db97fde3196d7c98c8b564
Signed-off-by: Pranith Kumar K <[email protected]>
  • Loading branch information
pranithk authored and xhernandez committed May 25, 2021
1 parent 68ed793 commit b666f60
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
67 changes: 67 additions & 0 deletions tests/bugs/locks/issue-2443-crash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <sys/file.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int
main(int argc, char *argv[])
{
int fd = -1;
char *filename = NULL;
struct flock lock = {
0,
};
int i = 0;
int ret = -1;

if (argc != 2) {
fprintf(stderr, "Usage: %s <filename> ", argv[0]);
goto out;
}

filename = argv[1];

fd = open(filename, O_RDWR | O_CREAT, 0);
if (fd < 0) {
fprintf(stderr, "open (%s) failed (%s)\n", filename, strerror(errno));
goto out;
}

lock.l_start = 0;
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_len = 2;

ret = fcntl(fd, F_SETLK, &lock);
if (ret < 0) {
fprintf(stderr, "fcntl setlk failed (%s)\n", strerror(errno));
goto out;
}

lock.l_start = 2;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_len = 2;

ret = fcntl(fd, F_SETLK, &lock);
if (ret < 0) {
fprintf(stderr, "fcntl setlk failed (%s)\n", strerror(errno));
goto out;
}

lock.l_start = 0;
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_len = 4;

ret = fcntl(fd, F_SETLK, &lock);
if (ret < 0) {
fprintf(stderr, "fcntl setlk failed (%s)\n", strerror(errno));
goto out;
}
out:
return ret;
}
18 changes: 18 additions & 0 deletions tests/bugs/locks/issue-2443-crash.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
. $(dirname $0)/../../include.rc
. $(dirname $0)/../../volume.rc
cleanup;

TEST glusterd
TEST pidof glusterd
TEST $CLI volume create $V0 $H0:$B0/brick0
TEST $CLI volume start $V0
TEST $GFS --volfile-id=$V0 --volfile-server=$H0 $M0;

build_tester $(dirname $0)/issue-2443-crash.c
TEST mv $(dirname $0)/issue-2443-crash $M0
cd $M0
TEST ./issue-2443-crash a

cd -
cleanup;
1 change: 1 addition & 0 deletions xlators/protocol/client/src/client-lk.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ add_locks(client_posix_lock_t *l1, client_posix_lock_t *l2)
sum = GF_CALLOC(1, sizeof(*sum), gf_client_mt_clnt_lock_t);
if (!sum)
return NULL;
INIT_LIST_HEAD(&sum->list);

sum->fl_start = min(l1->fl_start, l2->fl_start);
sum->fl_end = max(l1->fl_end, l2->fl_end);
Expand Down

0 comments on commit b666f60

Please sign in to comment.