Skip to content

Commit 7fb13f9

Browse files
committed
Fix warnings on format truncation and sizeof in strncpy #308
Debian 10 (Buster) has gcc 8.2 which warns about: source/c_pwm.c:459:65: error: argument to ‘sizeof’ in ‘strncpy’ call is the same expression as the source; did you mean to use the size of the destination? [-Werror=sizeof-pointer-memaccess] source/c_pwm.c:396:43: error: ‘%s’ directive output may be truncated writing up to 199 bytes into a region of size 100 [-Werror=format-truncation=]
1 parent 3a5d23d commit 7fb13f9

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

source/c_adc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ int adc_initialized = 0;
4141
BBIO_err initialize_adc(void)
4242
{
4343
#ifdef BBBVERSION41
44-
char test_path[49];
44+
char test_path[149];
4545
#else
46-
char test_path[40];
46+
char test_path[140];
4747
#endif
4848
FILE *fh;
4949
BBIO_err err;
@@ -94,10 +94,10 @@ BBIO_err read_value(unsigned int ain, float *value)
9494
{
9595
FILE * fh;
9696
#ifdef BBBVERSION41
97-
char ain_path[49];
97+
char ain_path[149];
9898
snprintf(ain_path, sizeof(ain_path), "%s%d_raw", adc_prefix_dir, ain);
9999
#else
100-
char ain_path[40];
100+
char ain_path[140];
101101
snprintf(ain_path, sizeof(ain_path), "%s%d", adc_prefix_dir, ain);
102102
#endif
103103

source/c_pwm.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ BBIO_err initialize_pwm(void)
119119

120120
BBIO_err pwm_set_frequency(const char *key, float freq) {
121121
int len;
122-
char buffer[20];
122+
char buffer[100];
123123
unsigned long period_ns;
124124
struct pwm_exp *pwm;
125125

@@ -195,7 +195,7 @@ BBIO_err pwm_set_frequency(const char *key, float freq) {
195195
// Only works before chip is enabled
196196
BBIO_err pwm_set_polarity(const char *key, int polarity) {
197197
int len;
198-
char buffer[9]; /* allow room for trailing NUL byte */
198+
char buffer[100]; /* allow room for trailing NUL byte */
199199
struct pwm_exp *pwm;
200200
#ifdef BBBVERSION41
201201
int enabled; /* Maintain original state */
@@ -275,7 +275,7 @@ BBIO_err pwm_set_polarity(const char *key, int polarity) {
275275

276276
BBIO_err pwm_set_duty_cycle(const char *key, float duty) {
277277
int len;
278-
char buffer[20];
278+
char buffer[100];
279279
struct pwm_exp *pwm;
280280

281281
if (duty < 0.0 || duty > 100.0)
@@ -309,17 +309,17 @@ BBIO_err pwm_setup(const char *key, __attribute__ ((unused)) float duty, __attri
309309
struct pwm_exp *new_pwm;
310310

311311
#ifdef BBBVERSION41
312-
char pwm_dev_path[45]; // "/sys/devices/platform/ocp/48300000.epwmss"
313-
char pwm_addr_path[60]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm"
314-
char pwm_chip_path[75]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm/pwm/pwmchip0"
315-
char pwm_export_path[80]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm/pwm/pwmchip0/export"
316-
char pwm_path[85]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm/pwm/pwmchip0/pwm1"
317-
char pwm_path_udev[85]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm/pwm/pwmchip0/pwm-0:1"
318-
char ecap_path_udev[85];// "/sys/devices/platform/ocp/48300000.epwmss/48300100.ecap/pwm/pwmchip0/pwm-0:0/"
319-
char duty_path[95]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm/pwm/pwmchip0/pwm1/duty_cycle"
320-
char period_path[95];
321-
char polarity_path[95];
322-
char enable_path[90];
312+
char pwm_dev_path[100]; // "/sys/devices/platform/ocp/48300000.epwmss"
313+
char pwm_addr_path[150]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm"
314+
char pwm_chip_path[200]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm/pwm/pwmchip0"
315+
char pwm_export_path[250]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm/pwm/pwmchip0/export"
316+
char pwm_path[250]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm/pwm/pwmchip0/pwm2"
317+
char pwm_path_udev[250]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm/pwm/pwmchip0/pwm-0:2"
318+
char ecap_path_udev[300]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ecap/pwm/pwmchip0/pwm-0:0/"
319+
char duty_path[300]; // "/sys/devices/platform/ocp/48300000.epwmss/48300200.ehrpwm/pwm/pwmchip0/pwm2/duty_cycle"
320+
char period_path[300];
321+
char polarity_path[300];
322+
char enable_path[300];
323323
char pin_mode[PIN_MODE_LEN]; // "pwm" or "pwm2"
324324

325325
int e;
@@ -400,7 +400,7 @@ BBIO_err pwm_setup(const char *key, __attribute__ ((unused)) float duty, __attri
400400
snprintf(pwm_path_udev, sizeof(pwm_path_udev), "%s/pwm-%c:%d", pwm_chip_path, pwm_path[66], p->index);
401401
syslog(LOG_DEBUG, "Adafruit_BBIO: pwm_start: key: %s, pwm_path_udev: %s", key, pwm_path_udev);
402402
//ecap output with udev patch
403-
snprintf(ecap_path_udev, sizeof(ecap_path_udev), "%s/pwm-%c:%d", pwm_chip_path, pwm_path[67], p->index);
403+
snprintf(ecap_path_udev, sizeof(ecap_path_udev), "%s/pwm-%c:%d", pwm_chip_path, pwm_path[66], p->index);
404404
syslog(LOG_DEBUG, "Adafruit_BBIO: pwm_start: key: %s, ecap_path_udev: %s", key, ecap_path_udev);
405405

406406
// Export PWM if hasn't already been
@@ -456,11 +456,11 @@ BBIO_err pwm_setup(const char *key, __attribute__ ((unused)) float duty, __attri
456456
return BBIO_GEN;
457457
}
458458
} else {
459-
strncpy(pwm_path, ecap_path_udev, sizeof(ecap_path_udev));
459+
strncpy(pwm_path, ecap_path_udev, sizeof(pwm_path));
460460
}
461461
}
462462
} else {
463-
strncpy(pwm_path, pwm_path_udev, sizeof(pwm_path_udev));
463+
strncpy(pwm_path, pwm_path_udev, sizeof(pwm_path));
464464
usleep(100*1000);
465465
}
466466
}
@@ -470,12 +470,12 @@ BBIO_err pwm_setup(const char *key, __attribute__ ((unused)) float duty, __attri
470470
snprintf(duty_path, sizeof(duty_path), "%s/duty_cycle", pwm_path);
471471
snprintf(enable_path, sizeof(enable_path), "%s/enable", pwm_path);
472472
#else
473-
char fragment[18];
474-
char pwm_fragment[20];
475-
char pwm_path[45];
476-
char duty_path[56];
477-
char period_path[50];
478-
char polarity_path[55];
473+
char fragment[100];
474+
char pwm_fragment[100];
475+
char pwm_path[100];
476+
char duty_path[200];
477+
char period_path[100];
478+
char polarity_path[100];
479479
int period_fd, duty_fd, polarity_fd;
480480

481481
if (!pwm_initialized) {
@@ -590,7 +590,7 @@ BBIO_err pwm_start(const char *key, float duty, float freq, int polarity)
590590
//fprintf(stderr, "Adafruit_BBIO: pwm_start: %s, %f, %f, %i\n", key, duty, freq, polarity);
591591

592592
BBIO_err err;
593-
char buffer[20];
593+
char buffer[100];
594594
ssize_t len;
595595

596596
struct pwm_exp *pwm = lookup_exported_pwm(key);
@@ -694,7 +694,7 @@ BBIO_err pwm_disable(const char *key)
694694

695695
#ifndef BBBVERSION41
696696
BBIO_err err;
697-
char fragment[18];
697+
char fragment[100];
698698
snprintf(fragment, sizeof(fragment), "bone_pwm_%s", key);
699699
err = unload_device_tree(fragment);
700700
if (err != BBIO_OK)
@@ -709,7 +709,7 @@ BBIO_err pwm_disable(const char *key)
709709
{
710710

711711
#ifdef BBBVERSION41
712-
char buffer[2];
712+
char buffer[100];
713713
size_t len;
714714

715715
// Disable the PWM

0 commit comments

Comments
 (0)