Skip to content

Commit 8d8ffef

Browse files
andre-rosaAl Viro
authored andcommitted
mqueue: only set error codes if they are really necessary
... postponing assignments until they're needed. Doesn't change code size. Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 04db0dd commit 8d8ffef

1 file changed

Lines changed: 50 additions & 27 deletions

File tree

‎ipc/mqueue.c‎

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
184184
{
185185
struct inode *inode;
186186
struct ipc_namespace *ns = data;
187-
int error = 0;
187+
int error;
188188

189189
sb->s_blocksize = PAGE_CACHE_SIZE;
190190
sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
@@ -202,7 +202,9 @@ static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
202202
if (!sb->s_root) {
203203
iput(inode);
204204
error = -ENOMEM;
205+
goto out;
205206
}
207+
error = 0;
206208

207209
out:
208210
return error;
@@ -621,9 +623,10 @@ static struct file *do_create(struct ipc_namespace *ipc_ns, struct dentry *dir,
621623
int ret;
622624

623625
if (attr) {
624-
ret = -EINVAL;
625-
if (!mq_attr_ok(ipc_ns, attr))
626+
if (!mq_attr_ok(ipc_ns, attr)) {
627+
ret = -EINVAL;
626628
goto out;
629+
}
627630
/* store for use during create */
628631
dentry->d_fsdata = attr;
629632
}
@@ -714,19 +717,21 @@ SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, mode_t, mode,
714717
if (oflag & O_CREAT) {
715718
if (dentry->d_inode) { /* entry already exists */
716719
audit_inode(name, dentry);
717-
error = -EEXIST;
718-
if (oflag & O_EXCL)
720+
if (oflag & O_EXCL) {
721+
error = -EEXIST;
719722
goto out;
723+
}
720724
filp = do_open(ipc_ns, dentry, oflag);
721725
} else {
722726
filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root,
723727
dentry, oflag, mode,
724728
u_attr ? &attr : NULL);
725729
}
726730
} else {
727-
error = -ENOENT;
728-
if (!dentry->d_inode)
731+
if (!dentry->d_inode) {
732+
error = -ENOENT;
729733
goto out;
734+
}
730735
audit_inode(name, dentry);
731736
filp = do_open(ipc_ns, dentry, oflag);
732737
}
@@ -873,19 +878,24 @@ SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,
873878
audit_mq_sendrecv(mqdes, msg_len, msg_prio, p);
874879
timeout = prepare_timeout(p);
875880

876-
ret = -EBADF;
877881
filp = fget(mqdes);
878-
if (unlikely(!filp))
882+
if (unlikely(!filp)) {
883+
ret = -EBADF;
879884
goto out;
885+
}
880886

881887
inode = filp->f_path.dentry->d_inode;
882-
if (unlikely(filp->f_op != &mqueue_file_operations))
888+
if (unlikely(filp->f_op != &mqueue_file_operations)) {
889+
ret = -EBADF;
883890
goto out_fput;
891+
}
884892
info = MQUEUE_I(inode);
885893
audit_inode(NULL, filp->f_path.dentry);
886894

887-
if (unlikely(!(filp->f_mode & FMODE_WRITE)))
895+
if (unlikely(!(filp->f_mode & FMODE_WRITE))) {
896+
ret = -EBADF;
888897
goto out_fput;
898+
}
889899

890900
if (unlikely(msg_len > info->attr.mq_msgsize)) {
891901
ret = -EMSGSIZE;
@@ -962,19 +972,24 @@ SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
962972
audit_mq_sendrecv(mqdes, msg_len, 0, p);
963973
timeout = prepare_timeout(p);
964974

965-
ret = -EBADF;
966975
filp = fget(mqdes);
967-
if (unlikely(!filp))
976+
if (unlikely(!filp)) {
977+
ret = -EBADF;
968978
goto out;
979+
}
969980

970981
inode = filp->f_path.dentry->d_inode;
971-
if (unlikely(filp->f_op != &mqueue_file_operations))
982+
if (unlikely(filp->f_op != &mqueue_file_operations)) {
983+
ret = -EBADF;
972984
goto out_fput;
985+
}
973986
info = MQUEUE_I(inode);
974987
audit_inode(NULL, filp->f_path.dentry);
975988

976-
if (unlikely(!(filp->f_mode & FMODE_READ)))
989+
if (unlikely(!(filp->f_mode & FMODE_READ))) {
990+
ret = -EBADF;
977991
goto out_fput;
992+
}
978993

979994
/* checks if buffer is big enough */
980995
if (unlikely(msg_len < info->attr.mq_msgsize)) {
@@ -1064,13 +1079,14 @@ SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
10641079

10651080
/* create the notify skb */
10661081
nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
1067-
ret = -ENOMEM;
1068-
if (!nc)
1082+
if (!nc) {
1083+
ret = -ENOMEM;
10691084
goto out;
1070-
ret = -EFAULT;
1085+
}
10711086
if (copy_from_user(nc->data,
10721087
notification.sigev_value.sival_ptr,
10731088
NOTIFY_COOKIE_LEN)) {
1089+
ret = -EFAULT;
10741090
goto out;
10751091
}
10761092

@@ -1079,9 +1095,10 @@ SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
10791095
/* and attach it to the socket */
10801096
retry:
10811097
filp = fget(notification.sigev_signo);
1082-
ret = -EBADF;
1083-
if (!filp)
1098+
if (!filp) {
1099+
ret = -EBADF;
10841100
goto out;
1101+
}
10851102
sock = netlink_getsockbyfilp(filp);
10861103
fput(filp);
10871104
if (IS_ERR(sock)) {
@@ -1093,7 +1110,7 @@ SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
10931110
timeo = MAX_SCHEDULE_TIMEOUT;
10941111
ret = netlink_attachskb(sock, nc, &timeo, NULL);
10951112
if (ret == 1)
1096-
goto retry;
1113+
goto retry;
10971114
if (ret) {
10981115
sock = NULL;
10991116
nc = NULL;
@@ -1102,14 +1119,17 @@ SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
11021119
}
11031120
}
11041121

1105-
ret = -EBADF;
11061122
filp = fget(mqdes);
1107-
if (!filp)
1123+
if (!filp) {
1124+
ret = -EBADF;
11081125
goto out;
1126+
}
11091127

11101128
inode = filp->f_path.dentry->d_inode;
1111-
if (unlikely(filp->f_op != &mqueue_file_operations))
1129+
if (unlikely(filp->f_op != &mqueue_file_operations)) {
1130+
ret = -EBADF;
11121131
goto out_fput;
1132+
}
11131133
info = MQUEUE_I(inode);
11141134

11151135
ret = 0;
@@ -1172,14 +1192,17 @@ SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
11721192
return -EINVAL;
11731193
}
11741194

1175-
ret = -EBADF;
11761195
filp = fget(mqdes);
1177-
if (!filp)
1196+
if (!filp) {
1197+
ret = -EBADF;
11781198
goto out;
1199+
}
11791200

11801201
inode = filp->f_path.dentry->d_inode;
1181-
if (unlikely(filp->f_op != &mqueue_file_operations))
1202+
if (unlikely(filp->f_op != &mqueue_file_operations)) {
1203+
ret = -EBADF;
11821204
goto out_fput;
1205+
}
11831206
info = MQUEUE_I(inode);
11841207

11851208
spin_lock(&info->lock);

0 commit comments

Comments
 (0)