fake_outputs: Use %n format specifier instead of sprintf
fake_outputs_init used a sprintf invocation with a throw-away buffer to estimate how many characters the sscanf invocation consumed. This was unnecessary, and also potentially incorrect, as differences between the read and formatted strings (such as leading zeros) could lead to fake_outputs_init to lose its track. Instead, use the %n format specifier which allows saving the number of characters consumed by sscanf so far. %n is part of C99.
This commit is contained in:
parent
da72a5be91
commit
4ddf18ef6b
|
@ -33,10 +33,10 @@ static Output *get_screen_at(unsigned int x, unsigned int y) {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void fake_outputs_init(const char *output_spec) {
|
void fake_outputs_init(const char *output_spec) {
|
||||||
char useless_buffer[1024];
|
|
||||||
const char *walk = output_spec;
|
const char *walk = output_spec;
|
||||||
unsigned int x, y, width, height;
|
unsigned int x, y, width, height;
|
||||||
while (sscanf(walk, "%ux%u+%u+%u", &width, &height, &x, &y) == 4) {
|
int chars_consumed;
|
||||||
|
while (sscanf(walk, "%ux%u+%u+%u%n", &width, &height, &x, &y, &chars_consumed) == 4) {
|
||||||
DLOG("Parsed output as width = %u, height = %u at (%u, %u)\n",
|
DLOG("Parsed output as width = %u, height = %u at (%u, %u)\n",
|
||||||
width, height, x, y);
|
width, height, x, y);
|
||||||
Output *new_output = get_screen_at(x, y);
|
Output *new_output = get_screen_at(x, y);
|
||||||
|
@ -68,8 +68,7 @@ void fake_outputs_init(const char *output_spec) {
|
||||||
num_screens++;
|
num_screens++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Figure out how long the input was to skip it */
|
walk += chars_consumed + 1;
|
||||||
walk += sprintf(useless_buffer, "%ux%u+%u+%u", width, height, x, y) + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (num_screens == 0) {
|
if (num_screens == 0) {
|
||||||
|
|
Loading…
Reference in New Issue