When running the following test program on the ARM
development board, “Write Failed Bad address
” would appear probabilistic:
/* Write test */
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(int argc,char* argv[]){
int i = 0;
int w_rw = 0;
int fd = -1;
int bw = -1;
int nob = 0;
char* file_name = "testfile";
char buff[30] = "Testing";
w_rw = 1; // 1 - write , 2 - rewrite
nob = 1024; // No of bytes to write
for(i = 0; i < w_rw; i++){
if((fd = open(file_name,O_CREAT | O_RDWR))< 0){
perror("Open failed!");
return -1;
}
printf("fd:%d",fd);
if((bw = write(fd,buff,nob)) < 0){
perror("Write failed!");
return -1;
}
printf("\n Bytes written : %d \n",bw);
}
return 0;
}
write()
function man
manual. The description of this function is as follows:
Function definition: ssize_t write (int fd, const void * buf, size_t count);
Function Description: write() writes a count of bytes from the memory specified by buf to the file specified by fd.
Return Value: If write() succeeds, it returns the actual number of bytes written (len). When an error occurs, it returns -1, and the error code is stored in errno.
Here the byte written from the buf
file fd
file is determined by the count
, not the actual allocated memory of the buf
file. Thus, there is a case for accessing invalid addresses (Bad address</code b>) : if the value of
count
is greater than the actual allocated memory size of buf
, the write()
function may attempt to access invalid addresses beyond the actual memory space of buf
.
The problem with the above test code is that buf
is small, only 30 bytes, and write()
is trying to write 1024 bytes, which will probabilistically access the invalid address. This problem can be solved by simply modifying the following code:
if((bw = write(fd,buff,sizeof(buf))) < 0){
perror("Write failed!");
return -1;
}
Read More:
- C#: Analysis of the difference between write() and writeline()
- Could not write JSON: write javaBean error, fastjson version x.x.x, class
- The function and usage of argc and argv in C language
- The C language qsort() function reports an error for overflow of – 2147483648 and 2147483648
- R language – error analysis – error in Call.graphics (C_ palette2, .Call(C_ palette2, NULL)) : invalid graphics state
- App installation failed.could not write to the device.
- linux Run: initramfs unpacking failed:write error
- SSH write failed broken pipe
- java.net.SocketException: Broken pipe (Write failed)
- Error c2137 of C language: empty character constant (Fixed)
- error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)
- C language string processing error warning, c4996, sprintf, predicted, c4996, strcpy, c4996, strcat
- After installation, Ubuntu encountered [SDB] asking for cache data failed assembling drive cache: write through
- Write about the problems and solutions when configuring OpenGL in vs2015
- Error analysis of multiple linear regression in R language model.frame.default
- [Linux] e297: write error in swap file solution
- [kimol Jun’s boring little invention] – using Python to write paper downloader (graphical interface)
- Analysis of R language error replacement has length zero problem
- Failed to write output file ‘C:’ windows\ Microsoft.NET \Framework64\v4.0.30319\Temporary ASP.NET Files\root\106f9ae8\cc0e1
- write error in swap file problem solving