Skip to content

Commit

Permalink
Z: Fix atoe_vsnprintf to support null buffer
Browse files Browse the repository at this point in the history
Std vsnprintf accepts null buffer and returns an estimate
Our implementation does not support that
Updated the atoe_vsnprintf implementation

Signed-off-by: ehsan kiani far ehsan.kianifar@gmail.com
  • Loading branch information
ehsankianifar committed Oct 26, 2023
1 parent 071c0c2 commit 38790e3
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions util/a2e/atoe_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <stdio.h>
#include <stdlib.h> /* for malloc() via e2a_string()/a2e_string() */
#include <string.h>
#include <limits.h>

/*
* ======================================================================
Expand All @@ -60,6 +61,7 @@
typedef struct InstanceData {
char *buffer;
char *end;
int counter;
} InstanceData;

/**************************************************************************
Expand Down Expand Up @@ -100,11 +102,12 @@ pchar(InstanceData *this, int c)
*this->buffer++ = '^';
}
#endif /* 0 */
this->counter++;

if (this->buffer >= this->end) {
return ERROR_RETVAL;
if (this->buffer != NULL && this->buffer < this->end) {
*this->buffer++ = c;
}
*this->buffer++ = c;

return SUCCESS;
}

Expand Down Expand Up @@ -418,18 +421,18 @@ atoe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
bool_t fPrecision;
int min_width, precision, ch;
static char NULLCHARSTRING[] = "[null]"; /*ibm@029013*/
this.counter = 0;
/*ibm@029013*/
if (fmt == NULL) { /*ibm@029013*/
fmt = NULLCHARSTRING; /*ibm@029013*/
} /*ibm@029013*/
if (str == NULL) {
return ERROR_RETVAL;
}
str[0] = '\0';

this.buffer = str;
this.end = str + count - 1;
*this.end = '\0'; /* ensure null-termination in case of failure */
this.end = this.buffer + count - 1;

if(this.buffer != NULL){
this.buffer[0] = '\0';
*this.end = '\0'; /* ensure null-termination in case of failure */
}

while ((ch = *fmt++) != 0) {
if (ch == '%') {
Expand All @@ -440,7 +443,7 @@ atoe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
pattern = fmt - 1; /*ibm@8665*/
left_justify = TRUE;
min_width = 0;
precision = this.end - this.buffer;
precision = INT_MAX;

next_char:
ch = *fmt++;
Expand Down Expand Up @@ -545,22 +548,22 @@ atoe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
/* ibm@8665
* Add floating point support for dbl2str & flt2str
*/
char *b;
int len;
char *tempPattern;

b = a2e((char *)pattern, fmt - pattern);
tempPattern = a2e((char *)pattern, fmt - pattern);

/* Extract a double from args, this works for both doubles
* and floats,
* NB if we use float for a single precision floating
* point number the result is wrong.
*/
len = sprintf(this.buffer, b, va_arg(args, double));
free(b);
b = e2a_string(this.buffer);
strcpy(this.buffer, b);
free(b);
this.buffer += len;
char *tempBuff = malloc(20 * sizeof(char));
sprintf(tempBuff, tempPattern, va_arg(args, double));
free(tempPattern);
tempPattern = e2a_string(tempBuff);
free(tempBuff);
CheckRet(fstring(&this, tempPattern, FALSE, 0, INT_MAX));
free(tempPattern);
}
break;
default:
Expand All @@ -584,8 +587,10 @@ atoe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
CheckRet(pchar(&this, ch));
}
}
*this.buffer = '\0';
return strlen(str);
if(this.buffer != NULL){
*this.buffer = '\0';
}
return this.counter;
}

/* END OF FILE */

0 comments on commit 38790e3

Please sign in to comment.