Skip to content

Commit 681a893

Browse files
committed
misc: don't ignore return value of asprintf
Signed-off-by: Yuxuan Shui <[email protected]>
1 parent c3e18a6 commit 681a893

File tree

4 files changed

+69
-68
lines changed

4 files changed

+69
-68
lines changed

src/picom.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include "utils/list.h"
6969
#include "utils/misc.h"
7070
#include "utils/statistics.h"
71+
#include "utils/str.h"
7172
#include "utils/uthash_extra.h"
7273
#include "vblank.h"
7374
#include "wm/defs.h"
@@ -1150,10 +1151,7 @@ static int register_cm(session_t *ps) {
11501151
xcb_atom_t atom;
11511152

11521153
char *buf = NULL;
1153-
if (asprintf(&buf, "%s%d", register_prop, ps->c.screen) < 0) {
1154-
log_fatal("Failed to allocate memory");
1155-
return -1;
1156-
}
1154+
casprintf(&buf, "%s%d", register_prop, ps->c.screen);
11571155
atom = get_atom_with_nul(ps->atoms, buf, ps->c.c);
11581156
free(buf);
11591157

src/transition/curve.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,20 @@ struct curve parse_steps(const char *input_str, const char **out_end, char **err
126126
const char *str = input_str;
127127
*err = NULL;
128128
if (*str != '(') {
129-
asprintf(err, "Invalid steps %s.", str);
129+
casprintf(err, "Invalid steps %s.", str);
130130
return CURVE_INVALID_INIT;
131131
}
132132
str += 1;
133133
str = skip_space(str);
134134
char *end;
135135
auto steps = strtol(str, &end, 10);
136136
if (end == str || steps > INT_MAX) {
137-
asprintf(err, "Invalid step count at \"%s\".", str);
137+
casprintf(err, "Invalid step count at \"%s\".", str);
138138
return CURVE_INVALID_INIT;
139139
}
140140
str = skip_space(end);
141141
if (*str != ',') {
142-
asprintf(err, "Invalid steps argument list \"%s\".", input_str);
142+
casprintf(err, "Invalid steps argument list \"%s\".", input_str);
143143
return CURVE_INVALID_INIT;
144144
}
145145
str = skip_space(str + 1);
@@ -148,13 +148,13 @@ struct curve parse_steps(const char *input_str, const char **out_end, char **err
148148
bool jump_end =
149149
starts_with(str, "jump-end", true) || starts_with(str, "jump-both", true);
150150
if (!jump_start && !jump_end && !starts_with(str, "jump-none", true)) {
151-
asprintf(err, "Invalid jump setting for steps \"%s\".", str);
151+
casprintf(err, "Invalid jump setting for steps \"%s\".", str);
152152
return CURVE_INVALID_INIT;
153153
}
154154
str += jump_start ? (jump_end ? 9 : 10) : (jump_end ? 8 : 9);
155155
str = skip_space(str);
156156
if (*str != ')') {
157-
asprintf(err, "Invalid steps argument list \"%s\".", input_str);
157+
casprintf(err, "Invalid steps argument list \"%s\".", input_str);
158158
return CURVE_INVALID_INIT;
159159
}
160160
*out_end = str + 1;
@@ -165,7 +165,7 @@ struct curve parse_cubic_bezier(const char *input_str, const char **out_end, cha
165165
double numbers[4];
166166
const char *str = input_str;
167167
if (*str != '(') {
168-
asprintf(err, "Invalid cubic-bazier %s.", str);
168+
casprintf(err, "Invalid cubic-bazier %s.", str);
169169
return CURVE_INVALID_INIT;
170170
}
171171
str += 1;
@@ -175,13 +175,13 @@ struct curve parse_cubic_bezier(const char *input_str, const char **out_end, cha
175175
const char *end = NULL;
176176
numbers[i] = strtod_simple(str, &end);
177177
if (end == str) {
178-
asprintf(err, "Invalid number %s.", str);
178+
casprintf(err, "Invalid number %s.", str);
179179
return CURVE_INVALID_INIT;
180180
}
181181
str = skip_space(end);
182182
const char expected = i == 3 ? ')' : ',';
183183
if (*str != expected) {
184-
asprintf(err, "Invalid cubic-bazier argument list %s.", input_str);
184+
casprintf(err, "Invalid cubic-bazier argument list %s.", input_str);
185185
return CURVE_INVALID_INIT;
186186
}
187187
str += 1;
@@ -209,7 +209,7 @@ struct curve curve_parse(const char *str, const char **end, char **err) {
209209
return curve_parsers[i].parse(str + name_len, end, err);
210210
}
211211
}
212-
asprintf(err, "Unknown curve type \"%s\".", str);
212+
casprintf(err, "Unknown curve type \"%s\".", str);
213213
return CURVE_INVALID_INIT;
214214
}
215215

src/transition/script.c

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static char parse_op(const char *input_str, const char **end, char **err) {
143143
return input_str[0];
144144
}
145145

146-
asprintf(err, "Expected one of \"%s\", got '%c'.", operators, input_str[0]);
146+
casprintf(err, "Expected one of \"%s\", got '%c'.", operators, input_str[0]);
147147
*end = input_str;
148148
return 0;
149149
}
@@ -198,7 +198,7 @@ parse_raw_operand(struct expression_parser_context *ctx, const char *str, const
198198
}
199199
}
200200
if (!succeeded) {
201-
asprintf(err, "Expected a number or a variable name, got \"%s\".", str);
201+
casprintf(err, "Expected a number or a variable name, got \"%s\".", str);
202202
*end = str;
203203
return;
204204
}
@@ -219,7 +219,7 @@ parse_raw_operand(struct expression_parser_context *ctx, const char *str, const
219219
};
220220
ctx->need_context = true;
221221
} else {
222-
asprintf(err, "variable name \"%.*s\" is not defined", (int)(*end - str), str);
222+
casprintf(err, "variable name \"%.*s\" is not defined", (int)(*end - str), str);
223223
*end = str;
224224
return;
225225
}
@@ -245,8 +245,8 @@ static inline double op_eval(double l, enum op op, double r) {
245245
}
246246
static bool pop_op(const char *input_str, struct expression_parser_context *ctx, char **err) {
247247
if (ctx->operand_top < 2) {
248-
asprintf(err, "Missing operand for operator %c, in expression %s",
249-
ctx->op_stack[ctx->op_top - 1], input_str);
248+
casprintf(err, "Missing operand for operator %c, in expression %s",
249+
ctx->op_stack[ctx->op_top - 1], input_str);
250250
return false;
251251
}
252252
auto f = ctx->entry->entry_point;
@@ -298,7 +298,7 @@ static bool parse_operand_or_paren(struct expression_parser_context *ctx,
298298
}
299299
}
300300
if (ctx->op_top == 0) {
301-
asprintf(err, "Unmatched ')' in expression \"%s\"", input_str);
301+
casprintf(err, "Unmatched ')' in expression \"%s\"", input_str);
302302
return false;
303303
}
304304
ctx->op_top -= 1;
@@ -379,7 +379,7 @@ static bool expression_compile(struct compilation_stack **stack_entry, const cha
379379
}
380380
}
381381
if (ctx.operand_top != 1) {
382-
asprintf(err, "excessive operand on stack %s", input_str);
382+
casprintf(err, "excessive operand on stack %s", input_str);
383383
goto end;
384384
}
385385
succeeded = true;
@@ -439,8 +439,8 @@ transition_compile(struct compilation_stack **stack_entry, config_setting_t *set
439439
if (config_setting_lookup_string(setting, "curve", &str)) {
440440
curve = curve_parse(str, &str, &err);
441441
if (curve.type == CURVE_INVALID) {
442-
asprintf(out_err, "Cannot parse curve at line %d: %s",
443-
config_setting_source_line(setting), err);
442+
casprintf(out_err, "Cannot parse curve at line %d: %s",
443+
config_setting_source_line(setting), err);
444444
free(err);
445445
return false;
446446
}
@@ -467,14 +467,14 @@ transition_compile(struct compilation_stack **stack_entry, config_setting_t *set
467467
if (config_setting_lookup_float(setting, "start", &number)) {
468468
start = make_imm_stack_entry(ctx, number, start_slot, true);
469469
} else if (!config_setting_lookup_string(setting, "start", &str)) {
470-
asprintf(out_err,
471-
"Transition definition does not contain a start value or "
472-
"expression. Line %d.",
473-
config_setting_source_line(setting));
470+
casprintf(out_err,
471+
"Transition definition does not contain a start value or "
472+
"expression. Line %d.",
473+
config_setting_source_line(setting));
474474
return false;
475475
} else if (!expression_compile(&start, str, ctx, start_slot, !reset, &err)) {
476-
asprintf(out_err, "transition has an invalid start expression: %s Line %d.",
477-
err, config_setting_source_line(setting));
476+
casprintf(out_err, "transition has an invalid start expression: %s Line %d.",
477+
err, config_setting_source_line(setting));
478478
free(err);
479479
return false;
480480
}
@@ -487,18 +487,18 @@ transition_compile(struct compilation_stack **stack_entry, config_setting_t *set
487487
.imm = number,
488488
};
489489
} else if (!config_setting_lookup_string(setting, "end", &str)) {
490-
asprintf(out_err,
491-
"Transition definition does not contain a end value or "
492-
"expression. Line %d.",
493-
config_setting_source_line(setting));
490+
casprintf(out_err,
491+
"Transition definition does not contain a end value or "
492+
"expression. Line %d.",
493+
config_setting_source_line(setting));
494494
return false;
495495
} else {
496496
BUG_ON(ctx->allocated_slots > UINT_MAX - 1);
497497
auto end_slot = ctx->allocated_slots++;
498498
if (!expression_compile(&end, str, ctx, end_slot, false, &err)) {
499-
asprintf(out_err,
500-
"Transition has an invalid end expression: %s. Line %d",
501-
err, config_setting_source_line(setting));
499+
casprintf(out_err,
500+
"Transition has an invalid end expression: %s. Line %d",
501+
err, config_setting_source_line(setting));
502502
free(err);
503503
return false;
504504
}
@@ -510,26 +510,28 @@ transition_compile(struct compilation_stack **stack_entry, config_setting_t *set
510510

511511
if (config_setting_lookup_float(setting, "duration", &number)) {
512512
if (number == 0) {
513-
asprintf(out_err, "Duration must be greater than 0. Line %d.",
514-
config_setting_source_line(setting));
513+
casprintf(out_err, "Duration must be greater than 0. Line %d.",
514+
config_setting_source_line(setting));
515515
return false;
516516
}
517517
load_parameters[1] = (struct instruction){
518518
.type = INST_IMM,
519519
.imm = number,
520520
};
521521
} else if (!config_setting_lookup_string(setting, "duration", &str)) {
522-
asprintf(out_err,
523-
"Transition definition does not contain a duration value or "
524-
"expression. Line %d.",
525-
config_setting_source_line(setting));
522+
casprintf(out_err,
523+
"Transition definition does not contain a duration value or "
524+
"expression. Line %d.",
525+
config_setting_source_line(setting));
526526
return false;
527527
} else {
528528
BUG_ON(ctx->allocated_slots > UINT_MAX - 1);
529529
auto duration_slot = ctx->allocated_slots++;
530530
if (!expression_compile(&end, str, ctx, duration_slot, false, &err)) {
531-
asprintf(out_err, "Transition has an invalid duration expression: %s. Line %d",
532-
err, config_setting_source_line(setting));
531+
casprintf(out_err,
532+
"Transition has an invalid duration expression: %s. "
533+
"Line %d",
534+
err, config_setting_source_line(setting));
533535
free(err);
534536
return false;
535537
}
@@ -553,8 +555,8 @@ transition_compile(struct compilation_stack **stack_entry, config_setting_t *set
553555
BUG_ON(ctx->allocated_slots > UINT_MAX - 1);
554556
auto delay_slot = ctx->allocated_slots++;
555557
if (!expression_compile(&end, str, ctx, delay_slot, false, &err)) {
556-
asprintf(out_err, "Transition has an invalid delay expression: %s. Line %d",
557-
err, config_setting_source_line(setting));
558+
casprintf(out_err, "Transition has an invalid delay expression: %s. Line %d",
559+
err, config_setting_source_line(setting));
558560
free(err);
559561
return false;
560562
}
@@ -730,18 +732,19 @@ static bool script_compile_one(struct compilation_stack **stack_entry, config_se
730732
bool succeeded =
731733
expression_compile(stack_entry, str, ctx, alloc->slot, false, &tmp_err);
732734
if (!succeeded) {
733-
asprintf(err, "Failed to parse expression at line %d. %s",
734-
config_setting_source_line(var), tmp_err);
735+
casprintf(err, "Failed to parse expression at line %d. %s",
736+
config_setting_source_line(var), tmp_err);
735737
free(tmp_err);
736738
}
737739
return succeeded;
738740
}
739741

740742
if (!config_setting_is_group(var)) {
741-
asprintf(err,
742-
"Invalid variable \"%s\", it must be either a number, a string, "
743-
"or a config group defining a transition.",
744-
config_setting_name(var));
743+
casprintf(err,
744+
"Invalid variable \"%s\", it must be either a number, a "
745+
"string, "
746+
"or a config group defining a transition.",
747+
config_setting_name(var));
745748
return false;
746749
}
747750
return transition_compile(stack_entry, var, ctx, alloc->slot, err);
@@ -772,8 +775,8 @@ static void report_cycle(struct compilation_stack **stack, unsigned top, unsigne
772775
}
773776
strcpy(pos, last_name);
774777

775-
asprintf(err, "Cyclic references detected in animation script defined at line %d: %s",
776-
config_setting_source_line(setting), buf);
778+
casprintf(err, "Cyclic references detected in animation script defined at line %d: %s",
779+
config_setting_source_line(setting), buf);
777780
free(buf);
778781
}
779782

src/utils/str.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,33 @@ static inline int uitostr(unsigned int n, char *buf) {
4242
return ret;
4343
}
4444

45+
/// Like `asprintf`, but it aborts the program if memory allocation fails.
46+
static inline size_t __attribute__((format(printf, 2, 3)))
47+
casprintf(char **strp, const char *fmt, ...) {
48+
va_list ap;
49+
va_start(ap, fmt);
50+
int ret = vasprintf(strp, fmt, ap);
51+
va_end(ap);
52+
53+
BUG_ON(ret < 0);
54+
return (size_t)ret;
55+
}
56+
4557
/// Convert a double into a string. Avoid using *printf functions to print floating points
4658
/// directly because they are locale dependent.
4759
static inline void dtostr(double n, char **buf) {
4860
BUG_ON(safe_isnan(n));
4961
BUG_ON(safe_isinf(n));
5062
if (fabs(n) > 1e9) {
5163
// The number is so big that it's not meaningful to keep decimal places.
52-
asprintf(buf, "%.0f", n);
64+
casprintf(buf, "%.0f", n);
5365
return;
5466
}
5567

5668
if (n > 0) {
57-
asprintf(buf, "%.0f.%03d", floor(n), (int)(fmod(n, 1) * 1000));
69+
casprintf(buf, "%.0f.%03d", floor(n), (int)(fmod(n, 1) * 1000));
5870
} else {
59-
asprintf(buf, "-%.0f.%03d", floor(-n), (int)(fmod(-n, 1) * 1000));
71+
casprintf(buf, "-%.0f.%03d", floor(-n), (int)(fmod(-n, 1) * 1000));
6072
}
6173
}
6274

@@ -94,15 +106,3 @@ static inline bool starts_with(const char *str, const char *needle, bool ignore_
94106
/// reallocates it if it's not big enough.
95107
int asnprintf(char **strp, size_t *capacity, const char *fmt, ...)
96108
__attribute__((format(printf, 3, 4)));
97-
98-
/// Like `asprintf`, but it aborts the program if memory allocation fails.
99-
static inline size_t __attribute__((format(printf, 2, 3)))
100-
casprintf(char **strp, const char *fmt, ...) {
101-
va_list ap;
102-
va_start(ap, fmt);
103-
int ret = vasprintf(strp, fmt, ap);
104-
va_end(ap);
105-
106-
BUG_ON(ret < 0);
107-
return (size_t)ret;
108-
}

0 commit comments

Comments
 (0)