Skip to content

Commit a5cc234

Browse files
authored
Merge pull request #2 from pengutronix/topic/fix-warnings
Fix Warnings, raise meson warning level
2 parents fab0d40 + 91c4768 commit a5cc234

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ project(
33
'c',
44
version : '2019.12.0',
55
default_options : [
6-
'warning_level=1',
6+
'warning_level=2',
77
],
88
license : '0BSD',
99
)

platsch.c

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
* https://raw.githubusercontent.com/dvdhrm/docs/master/drm-howto/modeset.c
2020
*/
2121

22+
#define _GNU_SOURCE
23+
2224
#include <assert.h>
2325
#include <ctype.h>
2426
#include <errno.h>
2527
#include <getopt.h>
2628
#include <libgen.h>
27-
#include <stdarg.h>
2829
#include <stdbool.h>
2930
#include <stdio.h>
3031
#include <stdlib.h>
@@ -111,29 +112,28 @@ struct modeset_dev {
111112
uint32_t crtc_id;
112113
};
113114

114-
void draw_buffer(struct modeset_dev *dev, char *dir, char *base)
115+
void draw_buffer(struct modeset_dev *dev, const char *dir, const char *base)
115116
{
116117
int fd_src;
117-
char filename[128];
118+
char *filename;
118119
ssize_t size;
119120
int ret;
120121

121122
/*
122123
* make it easy and load a raw file in the right format instead of
123124
* opening an (say) PNG and convert the image data to the right format.
124125
*/
125-
ret = snprintf(filename, sizeof(filename),
126-
"%s/%s-%ux%u-%s.bin",
126+
ret = asprintf(&filename, "%s/%s-%ux%u-%s.bin",
127127
dir, base, dev->width, dev->height, dev->format->name);
128-
if (ret >= sizeof(filename)) {
129-
error("Failed to fit filename into buffer\n");
128+
if (ret < 0) {
129+
error("Failed to allocate filename buffer\n");
130130
return;
131131
}
132132

133133
fd_src = open(filename, O_RDONLY | O_CLOEXEC);
134134
if (fd_src < 0) {
135135
error("Failed to open %s: %m\n", filename);
136-
return;
136+
goto out;
137137
}
138138

139139
size = readfull(fd_src, dev->map, dev->size);
@@ -151,6 +151,9 @@ void draw_buffer(struct modeset_dev *dev, char *dir, char *base)
151151
error("Failed to close image file\n");
152152
}
153153

154+
out:
155+
free(filename);
156+
154157
return;
155158
}
156159

@@ -160,8 +163,8 @@ static int drmprepare_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
160163
struct modeset_dev *dev)
161164
{
162165
drmModeEncoder *enc;
163-
unsigned int i, j;
164-
int32_t crtc_id;
166+
int i, j;
167+
uint32_t crtc_id;
165168
struct modeset_dev *iter;
166169

167170
/* first try the currently connected encoder+crtc */
@@ -181,16 +184,16 @@ static int drmprepare_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
181184
if (enc) {
182185
if (enc->crtc_id) {
183186
crtc_id = enc->crtc_id;
184-
assert(crtc_id >= 0);
187+
bool in_use = false;
185188

186189
for (iter = modeset_list; iter; iter = iter->next) {
187190
if (iter->crtc_id == crtc_id) {
188-
crtc_id = -1;
191+
in_use = true;
189192
break;
190193
}
191194
}
192195

193-
if (crtc_id > 0) {
196+
if (!in_use) {
194197
debug("encoder #%d uses crtc #%d\n",
195198
enc->encoder_id, enc->crtc_id);
196199
drmModeFreeEncoder(enc);
@@ -223,6 +226,8 @@ static int drmprepare_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
223226

224227
/* iterate all global CRTCs */
225228
for (j = 0; j < res->count_crtcs; ++j) {
229+
bool in_use = false;
230+
226231
/* check whether this CRTC works with the encoder */
227232
if (!(enc->possible_crtcs & (1 << j)))
228233
continue;
@@ -231,13 +236,13 @@ static int drmprepare_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
231236
crtc_id = res->crtcs[j];
232237
for (iter = modeset_list; iter; iter = iter->next) {
233238
if (iter->crtc_id == crtc_id) {
234-
crtc_id = -1;
239+
in_use = true;
235240
break;
236241
}
237242
}
238243

239244
/* we have found a CRTC, so save it and return */
240-
if (crtc_id >= 0) {
245+
if (!in_use) {
241246
debug("encoder #%d will use crtc #%d\n",
242247
enc->encoder_id, crtc_id);
243248
drmModeFreeEncoder(enc);
@@ -348,7 +353,7 @@ static char *get_normalized_conn_type_name(uint32_t connector_type)
348353

349354
static const struct platsch_format *platsch_format_find(const char *name)
350355
{
351-
int i;
356+
unsigned i;
352357

353358
for (i = 0; i < ARRAY_SIZE(platsch_formats); i++)
354359
if (!strcmp(platsch_formats[i].name, name))
@@ -363,7 +368,7 @@ static int set_env_connector_mode(drmModeConnector *conn,
363368
int ret, i = 0;
364369
u_int32_t width = 0, height = 0;
365370
const char *mode;
366-
char *connector_type_name, mode_env_name[32], fmt_specifier[32] = "";
371+
char *connector_type_name, *mode_env_name = NULL, fmt_specifier[32] = "";
367372
const struct platsch_format *format = NULL;
368373

369374
connector_type_name = get_normalized_conn_type_name(conn->connector_type);
@@ -373,12 +378,12 @@ static int set_env_connector_mode(drmModeConnector *conn,
373378
goto fallback;
374379
}
375380

376-
ret = snprintf(mode_env_name, sizeof(mode_env_name), "platsch_%s%u_mode",
381+
ret = asprintf(&mode_env_name, "platsch_%s%u_mode",
377382
connector_type_name, conn->connector_type_id);
378383
free(connector_type_name);
379-
if (ret >= sizeof(mode_env_name)) {
380-
error("failed to fit platsch env mode variable name into buffer\n");
381-
return -EFAULT;
384+
if (ret < 0) {
385+
error("failed to allocate platsch env mode variable\n");
386+
return -ENOMEM;
382387
}
383388

384389
/* check for connector mode configuration in environment */
@@ -391,7 +396,8 @@ static int set_env_connector_mode(drmModeConnector *conn,
391396
ret = sscanf(mode, "%ux%u@%s", &width, &height, fmt_specifier);
392397
if (ret < 2) {
393398
error("error while scanning %s for mode\n", mode_env_name);
394-
return -EFAULT;
399+
ret = -EFAULT;
400+
goto err_out;
395401
}
396402

397403
/* use first mode matching given resolution */
@@ -407,7 +413,8 @@ static int set_env_connector_mode(drmModeConnector *conn,
407413

408414
if (i == conn->count_modes) {
409415
error("no mode available matching %ux%u\n", width, height);
410-
return -ENOENT;
416+
ret = -ENOENT;
417+
goto err_out;
411418
}
412419

413420
format = platsch_format_find(fmt_specifier);
@@ -419,6 +426,8 @@ static int set_env_connector_mode(drmModeConnector *conn,
419426

420427
dev->format = format;
421428

429+
free(mode_env_name);
430+
422431
return 0;
423432

424433
fallback:
@@ -432,7 +441,12 @@ static int set_env_connector_mode(drmModeConnector *conn,
432441
debug("using default format %s for connector #%u\n", dev->format->name,
433442
conn->connector_id);
434443

435-
return 0;
444+
ret = 0;
445+
446+
err_out:
447+
free(mode_env_name);
448+
449+
return ret;
436450
}
437451

438452
static int drmprepare_connector(int fd, drmModeRes *res, drmModeConnector *conn,
@@ -483,7 +497,7 @@ static int drmprepare(int fd)
483497
{
484498
drmModeRes *res;
485499
drmModeConnector *conn;
486-
unsigned int i;
500+
int i;
487501
struct modeset_dev *dev;
488502
int ret;
489503

@@ -562,7 +576,6 @@ int main(int argc, char *argv[])
562576
{
563577
char **initsargv;
564578
int drmfd;
565-
char drmdev[128];
566579
struct modeset_dev *iter;
567580
bool pid1 = getpid() == 1;
568581
const char *dir = "/usr/share/platsch";
@@ -606,19 +619,21 @@ int main(int argc, char *argv[])
606619

607620
for (i = 0; i < 64; i++) {
608621
struct drm_mode_card_res res = {0};
622+
char *drmdev;
609623

610624
/*
611625
* XXX: Maybe use drmOpen instead?
612626
* (Where should name/busid come from?)
613627
* XXX: Loop through drm devices to find one with connectors.
614628
*/
615-
ret = snprintf(drmdev, sizeof(drmdev), DRM_DEV_NAME, DRM_DIR_NAME, i);
616-
if (ret >= sizeof(drmdev)) {
617-
error("Huh, device name overflowed buffer\n");
629+
ret = asprintf(&drmdev, DRM_DEV_NAME, DRM_DIR_NAME, i);
630+
if (ret < 0) {
631+
error("Huh, failed to allocate device name buffer\n");
618632
goto execinit;
619633
}
620634

621635
drmfd = open(drmdev, O_RDWR | O_CLOEXEC, 0);
636+
free(drmdev);
622637
if (drmfd < 0) {
623638
error("Failed to open drm device: %m\n");
624639
goto execinit;

0 commit comments

Comments
 (0)