Skip to content

Commit 79399c1

Browse files
committed
io: reverted some changes due to PR review
1 parent aee4b9d commit 79399c1

1 file changed

Lines changed: 33 additions & 48 deletions

File tree

src/io.c

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ nc_read(struct nc_session *session, char *buf, uint32_t count, uint32_t inact_ti
6464
{
6565
uint32_t readd = 0;
6666
ssize_t r = -1;
67-
int fd, interrupted, res;
67+
int fd, interrupted;
6868
struct timespec ts_inact_timeout;
6969

7070
assert(session);
@@ -115,38 +115,31 @@ nc_read(struct nc_session *session, char *buf, uint32_t count, uint32_t inact_ti
115115
#ifdef NC_ENABLED_SSH_TLS
116116
case NC_TI_SSH:
117117
/* read via libssh */
118-
res = ssh_channel_read(session->ti.libssh.channel, buf + readd, count - readd, 0);
119-
if (res == SSH_AGAIN) {
118+
r = ssh_channel_read(session->ti.libssh.channel, buf + readd, count - readd, 0);
119+
if (r == SSH_AGAIN) {
120120
r = 0;
121121
break;
122-
} else if (res == SSH_ERROR) {
122+
} else if (r == SSH_ERROR) {
123123
ERR(session, "Reading from the SSH channel failed (%s).", ssh_get_error(session->ti.libssh.session));
124124
session->status = NC_STATUS_INVALID;
125125
session->term_reason = NC_SESSION_TERM_OTHER;
126126
return -1;
127-
} else if (res == 0) {
128-
r = 0;
127+
} else if (r == 0) {
129128
if (ssh_channel_is_eof(session->ti.libssh.channel)) {
130129
ERR(session, "SSH channel unexpected EOF.");
131130
session->status = NC_STATUS_INVALID;
132131
session->term_reason = NC_SESSION_TERM_DROPPED;
133132
return -1;
134133
}
135134
break;
136-
} else {
137-
r = (ssize_t) res;
138135
}
139136
break;
140137

141138
case NC_TI_TLS:
142-
res = nc_tls_read_wrap(session, (unsigned char *)buf + readd, count - readd);
143-
if (res < 0) {
139+
r = nc_tls_read_wrap(session, (unsigned char *)buf + readd, count - readd);
140+
if (r < 0) {
144141
/* non-recoverable error */
145-
return -1;
146-
} else if ((uint32_t) res > (count - readd)) {
147-
return -1;
148-
} else {
149-
r = (ssize_t) res;
142+
return r;
150143
}
151144
break;
152145
#endif /* NC_ENABLED_SSH_TLS */
@@ -167,13 +160,9 @@ nc_read(struct nc_session *session, char *buf, uint32_t count, uint32_t inact_ti
167160
session->term_reason = NC_SESSION_TERM_OTHER;
168161
return -1;
169162
}
170-
} else if ((r < 0) || (r > (UINT32_MAX - readd))) {
171-
session->status = NC_STATUS_INVALID;
172-
session->term_reason = NC_SESSION_TERM_OTHER;
173-
return -1;
174163
} else {
175164
/* something read */
176-
readd += (uint32_t) r;
165+
readd += r;
177166

178167
/* reset inactive timeout */
179168
nc_timeouttime_get(&ts_inact_timeout, inact_timeout);
@@ -592,8 +581,7 @@ struct nc_wclb_arg {
592581
static int
593582
nc_write(struct nc_session *session, const void *buf, uint32_t count)
594583
{
595-
ssize_t c;
596-
int fd, interrupted, res;
584+
int c, fd, interrupted;
597585
uint32_t written = 0;
598586

599587
if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
@@ -617,9 +605,9 @@ nc_write(struct nc_session *session, const void *buf, uint32_t count)
617605
case NC_TI_UNIX:
618606
fd = session->ti_type == NC_TI_FD ? session->ti.fd.out : session->ti.unixsock.sock;
619607
c = write(fd, (char *)(buf + written), count - written);
620-
if ((c == -1) && (errno == EAGAIN)) {
608+
if ((c < 0) && (errno == EAGAIN)) {
621609
c = 0;
622-
} else if ((c == -1) && (errno == EINTR)) {
610+
} else if ((c < 0) && (errno == EINTR)) {
623611
c = 0;
624612
interrupted = 1;
625613
} else if (c < 0) {
@@ -640,20 +628,18 @@ nc_write(struct nc_session *session, const void *buf, uint32_t count)
640628
session->term_reason = NC_SESSION_TERM_DROPPED;
641629
return -1;
642630
}
643-
res = ssh_channel_write(session->ti.libssh.channel, (char *)(buf + written), count - written);
644-
if ((res == SSH_ERROR) || (res == -1) || ((uint32_t) res > (count - written))) {
631+
c = ssh_channel_write(session->ti.libssh.channel, (char *)(buf + written), count - written);
632+
if ((c == SSH_ERROR) || (c == -1)) {
645633
ERR(session, "SSH channel write failed.");
646634
return -1;
647635
}
648-
c = (ssize_t) res;
649636
break;
650637
case NC_TI_TLS:
651-
res = nc_tls_write_wrap(session, (const unsigned char *)(buf + written), count - written);
652-
if ((res < 0) || ((uint32_t) res > (count - written))) {
638+
c = nc_tls_write_wrap(session, (const unsigned char *)(buf + written), count - written);
639+
if (c < 0) {
653640
/* possible client dc, or some socket/TLS communication error */
654641
return -1;
655642
}
656-
c = (ssize_t) res;
657643
break;
658644
#endif /* NC_ENABLED_SSH_TLS */
659645
default:
@@ -666,10 +652,10 @@ nc_write(struct nc_session *session, const void *buf, uint32_t count)
666652
usleep(NC_TIMEOUT_STEP);
667653
}
668654

669-
written += (uint32_t) c;
655+
written += c;
670656
} while (written < count);
671657

672-
return (written > INT_MAX) ? -1 : (int) written;
658+
return written;
673659
}
674660

675661
/**
@@ -684,21 +670,21 @@ nc_write(struct nc_session *session, const void *buf, uint32_t count)
684670
static int
685671
nc_write_starttag_and_msg(struct nc_session *session, const void *buf, uint32_t count)
686672
{
687-
int ret = 0, r, bufsize;
673+
int ret = 0, r;
688674
char chunksize[24];
689675

690676
if (session->version == NC_VERSION_11) {
691-
bufsize = sprintf(chunksize, "\n#%" PRIu32 "\n", count);
677+
r = sprintf(chunksize, "\n#%" PRIu32 "\n", count);
692678

693-
r = nc_write(session, chunksize, bufsize);
694-
if ((r < 0) || (r > bufsize)) {
679+
r = nc_write(session, chunksize, r);
680+
if (r == -1) {
695681
return -1;
696682
}
697683
ret += r;
698684
}
699685

700686
r = nc_write(session, buf, count);
701-
if ((r < 0) || ((uint32_t) r > (UINT32_MAX - ret))) {
687+
if (r == -1) {
702688
return -1;
703689
}
704690
ret += r;
@@ -761,52 +747,51 @@ nc_write_clb_flush(struct nc_wclb_arg *warg)
761747
static ssize_t
762748
nc_write_clb(void *arg, const void *buf, uint32_t count, int xmlcontent)
763749
{
764-
ssize_t ret = 0;
765-
int c;
750+
ssize_t ret = 0, c;
766751
uint32_t l;
767752
struct nc_wclb_arg *warg = arg;
768753

769754
if (!buf) {
770755
c = nc_write_clb_flush(warg);
771-
if (c < 0) {
756+
if (c == -1) {
772757
return -1;
773758
}
774-
ret += (ssize_t) c;
759+
ret += c;
775760

776761
/* endtag */
777762
c = nc_write_endtag(warg->session);
778-
if ((c < 0) || ((ssize_t) c > (SSIZE_MAX - ret))) {
763+
if (c == -1) {
779764
return -1;
780765
}
781-
ret += (ssize_t) c;
766+
ret += c;
782767

783768
return ret;
784769
}
785770

786771
if (warg->len && (warg->len + count > WRITE_BUFSIZE)) {
787772
/* dump current buffer */
788773
c = nc_write_clb_flush(warg);
789-
if (c < 0) {
774+
if (c == -1) {
790775
return -1;
791776
}
792-
ret += (ssize_t) c;
777+
ret += c;
793778
}
794779

795780
if (!xmlcontent && (count > WRITE_BUFSIZE)) {
796781
/* write directly */
797782
c = nc_write_starttag_and_msg(warg->session, buf, count);
798-
if (c < 0) {
783+
if (c == -1) {
799784
return -1;
800785
}
801-
ret += (ssize_t) c;
786+
ret += c;
802787
} else {
803788
/* keep in buffer and write later */
804789
if (xmlcontent) {
805790
for (l = 0; l < count; l++) {
806791
if (warg->len + 5 >= WRITE_BUFSIZE) {
807792
/* buffer is full */
808793
c = nc_write_clb_flush(warg);
809-
if (c < 0) {
794+
if (c == -1) {
810795
return -1;
811796
}
812797
}

0 commit comments

Comments
 (0)