Skip to content

Commit

Permalink
fix line-continuation logic and loop (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjstevns committed Jan 17, 2011
1 parent 56bbb1e commit 3b5c24f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
19 changes: 10 additions & 9 deletions src/dbmail-imapsession.c
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,7 @@ int imap4_tokenizer_main(ImapSession *self, const char *buffer)

max = strlen(s);

TRACE(TRACE_DEBUG,"[%p] tokenize [%lu/%lu] [%s]", self, max, self->rbuff_size, buffer);
TRACE(TRACE_DEBUG,"[%p] tokenize [%lu/%lu] [%s]", self, max, self->ci->rbuff_size, buffer);

assert(max <= MAX_LINESIZE);

Expand All @@ -1928,7 +1928,7 @@ int imap4_tokenizer_main(ImapSession *self, const char *buffer)
inquote = 0;

// if we're not fetching string-literals it's safe to strip NL
if (self->rbuff_size == 0)
if (self->ci->rbuff_size == 0)
g_strchomp(s);

if (self->args[0]) {
Expand Down Expand Up @@ -1961,18 +1961,18 @@ int imap4_tokenizer_main(ImapSession *self, const char *buffer)

for (i = 0; i < max && s[i] && self->args_idx < MAX_ARGS - 1; i++) {
/* get bytes of string-literal */
if (self->rbuff_size > 0) {
if (self->ci->rbuff_size > 0) {
size_t got = strlen(buffer);

assert(got <= self->rbuff_size);
assert(got <= self->ci->rbuff_size);

if (! self->args[self->args_idx])
self->args[self->args_idx] = g_new0(gchar, self->rbuff_size+1);
self->args[self->args_idx] = g_new0(gchar, self->ci->rbuff_size+1);

strncat(self->args[self->args_idx], buffer, got);
self->rbuff_size -= got;
if (self->rbuff_size <= 0) {
TRACE(TRACE_DEBUG,"string-literal complete [%lu:%s]", self->rbuff_size, buffer);
self->ci->rbuff_size -= got;
if (self->ci->rbuff_size <= 0) {
TRACE(TRACE_DEBUG,"string-literal complete [%lu:%s]", self->ci->rbuff_size, buffer);
self->args_idx++;
i += got;
} else {
Expand Down Expand Up @@ -2058,14 +2058,15 @@ int imap4_tokenizer_main(ImapSession *self, const char *buffer)

/* check for {number}\0 */
if (s[i] == '{') {
self->rbuff_size = strtoul(&s[i + 1], &lastchar, 10);
self->ci->rbuff_size += strtoul(&s[i + 1], &lastchar, 10);

/* only continue if the number is followed by '}\0' */
TRACE(TRACE_DEBUG, "[%p] last char = %c", self, *lastchar);
if ((*lastchar == '+' && *(lastchar + 1) == '}' && *(lastchar + 2) == '\0') ||
(*lastchar == '}' && *(lastchar + 1) == '\0')) {
dbmail_imap_session_buff_printf(self, "+ OK gimme that string\r\n");
dbmail_imap_session_buff_flush(self);
self->ci->rbuff_size = 0;
return 0;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/dbmail-imapsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ typedef struct {

GString *buff; // output buffer

size_t rbuff_size; // size of string-literals

int parser_state;
char **args;
u64_t args_idx;
Expand Down
2 changes: 1 addition & 1 deletion src/dbmailtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ typedef struct {
char tls_wbuf[TLS_SEGMENT]; /* buffer to write during tls session */
size_t tls_wbuf_n; /* number of octets to write during tls session */

size_t rbuff_size; /* size of string-literals */
GString *read_buffer; /* input buffer */
size_t read_buffer_offset; /* input buffer offset */

Expand Down Expand Up @@ -383,7 +384,6 @@ typedef struct {
GList *args; /* command args (allocated char *) */

GString *rbuff; /* input buffer */
size_t rbuff_size; /* required number of octets (for string literal) */

char *username;
char *password;
Expand Down
8 changes: 4 additions & 4 deletions src/imap4.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ void imap_cb_read(void *arg)
return;
}
if (state & CLIENT_EOF) {
if (len < 1)
if (0 < len < session->ci->rbuff_size)
imap_session_bailout(session);
}
if (session->ci->read_buffer->len > 0)
if (len > 0)
imap_handle_input(session);
}

Expand Down Expand Up @@ -349,10 +349,10 @@ void imap_handle_input(ImapSession *session)

memset(buffer, 0, sizeof(buffer));

if (session->rbuff_size <= 0) {
if (session->ci->rbuff_size <= 0) {
l = ci_readln(session->ci, buffer);
} else {
int needed = MIN(session->rbuff_size, (int)sizeof(buffer)-1);
int needed = MIN(session->ci->rbuff_size, (int)sizeof(buffer)-1);
l = ci_read(session->ci, buffer, needed);
}

Expand Down
12 changes: 6 additions & 6 deletions src/timsieve.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,17 @@ int tims_tokenizer(ClientSession_t *session, char *buffer)
session->command_type = command_type;
}

if (session->rbuff_size) {
if (session->ci->rbuff_size) {
size_t l = strlen(buffer);
size_t n = min(session->rbuff_size, l);
size_t n = min(session->ci->rbuff_size, l);
g_string_append_len(session->rbuff, buffer, n);
session->rbuff_size -= n;
if (! session->rbuff_size) {
session->ci->rbuff_size -= n;
if (! session->ci->rbuff_size) {
session->args = g_list_append(session->args, g_strdup(session->rbuff->str));
g_string_printf(session->rbuff,"%s","");
session->parser_state = TRUE;
}
TRACE(TRACE_DEBUG, "state [%d], size [%ld]", session->parser_state, session->rbuff_size);
TRACE(TRACE_DEBUG, "state [%d], size [%ld]", session->parser_state, session->ci->rbuff_size);
return session->parser_state;
}

Expand Down Expand Up @@ -259,7 +259,7 @@ int tims_tokenizer(ClientSession_t *session, char *buffer)
continue;
}
if (c == '{') { // a literal...
session->rbuff_size = strtoull(s+i, NULL, 10);
session->ci->rbuff_size = strtoull(s+i, NULL, 10);
return FALSE;
}
g_string_append_c(t,c);
Expand Down

0 comments on commit 3b5c24f

Please sign in to comment.